Setting Min and Max?
BackUpAndDown
Member Posts: 685
Hey could someone explain how to use the "min" and "max" commands?
Thanks ^_^
Thanks ^_^
Comments
if attribute self.positionX doesn't equal min(50,max(0,150)) then...
Right?
They work very simply like this:
min(number1, number2) will compare number1 and number2 and return to you the smaller one.
max(number1, number2) will compare number1 and number2 and return to you the larger one.
That doesn't seem all that useful, so here is how you would use it:
Let's say you want to be able to drag an actor on the X axis away from a certain point. Like a slingshot.
But you don't want to be able to drag the Actor more than 300 pixels.
You could just set up your dragging code normally, and then put a Rule that checks if the distance is more than 300. If it is more than 300, then Constrain it to 300.
That would look like this:
self.maxDistance = 300
Rule
When Touch is Pressed
.....Change Attribute: self.dragging To True
Otherwise
.....Change Attribute: self.dragging To False
Rule
When self.dragging = true
.....Constrain Attribute: self.Position.X To: Mouse.X
Rule
When self.maxDistance < (game.slingshotBaseX - self.Position.X )
.....Constrain Attribute: self.Position.X To: (game.slingshotBaseX - self.Position.X )
Which will work. But now you have an extra Rule and an extra Constrain. Both of which are processor intensive.
A better way would be:
self.maxDistance = 300
Rule
When Touch is Pressed
.....Change Attribute: self.dragging To True
Otherwise
.....Change Attribute: self.dragging To False
Rule
When self.dragging = true
.....Constrain Attribute: self.Position.X To: min(Mouse.X,(game.slingshotBaseX - self.Position.X ))
You can also combine min and max together like this:
Let's say you have a slider that controls the volume in the game. You could set it up like this:
Rule
When Touch is Pressed
.....Change Attribute: self.dragging To True
Otherwise
.....Change Attribute: self.dragging To False
Rule
When self.dragging = true
.....Constrain Attribute: self.Position.X To: max(self.leftLimit,min(self.rightLimit,Mouse.X))
That code will allow to drag a slider without using other actors as physical walls, or extraneous Rules to correct the position.
Similar code can be used to eliminate invisible, unmovable Actors set up as walls on the sides of your game to keep Actors within a certain area. That's 4 less Actors you'll need in the physics system, freeing up more resources.
Daren.