Stopping actors going off screen - How to
quantumsheep
Member Posts: 8,188
Hey all, hope you're well.
I've seen some discussion here (e.g. http://gamesalad.com/forums/topic.php?id=140 ) about keeping an actor on-screen.
A lot of suggestions have included using invisible walls, but I always thought that was a bit of a hack, and adds extra actors you might not need.
My solution is this.
The iphone screen size is X: 480 Y:320
On the actor you want to stay within these screen limits, use these rules.
Constrain the actors X and Y positions to the edge of the screen by using rules to check if the actor is beyond this and then moving the actor back on.
Hopefully these screenshots will be clearer:
http://quantumsheep.googlepages.com/Screenconstraints1.jpg
Here you can see that if the X position of the actor is less than 10, then we constrain the actor's self position to 10.
Additionally, if his position is greater than 470 then it is constrained to 470.
We also do the same with the Y position of the actor like this:
http://quantumsheep.googlepages.com/screenconstraints2.jpg
You can experiment with how close you want the actor to get to the edge. Also, you can remove the constraint and add a move attribute to make them move *away* from the edge (if it's an NPC for example).
This can also be used to destroy an actor once they've gone off the screen with a little tweaking.
Hope that's clear, and that it helps.
Cheers,
QS
I've seen some discussion here (e.g. http://gamesalad.com/forums/topic.php?id=140 ) about keeping an actor on-screen.
A lot of suggestions have included using invisible walls, but I always thought that was a bit of a hack, and adds extra actors you might not need.
My solution is this.
The iphone screen size is X: 480 Y:320
On the actor you want to stay within these screen limits, use these rules.
Constrain the actors X and Y positions to the edge of the screen by using rules to check if the actor is beyond this and then moving the actor back on.
Hopefully these screenshots will be clearer:
http://quantumsheep.googlepages.com/Screenconstraints1.jpg
Here you can see that if the X position of the actor is less than 10, then we constrain the actor's self position to 10.
Additionally, if his position is greater than 470 then it is constrained to 470.
We also do the same with the Y position of the actor like this:
http://quantumsheep.googlepages.com/screenconstraints2.jpg
You can experiment with how close you want the actor to get to the edge. Also, you can remove the constraint and add a move attribute to make them move *away* from the edge (if it's an NPC for example).
This can also be used to destroy an actor once they've gone off the screen with a little tweaking.
Hope that's clear, and that it helps.
Cheers,
QS
Dr. Sam Beckett never returned home...
Twitter: https://twitter.com/Quantum_Sheep
Web: https://quantumsheep.itch.io
Comments
I would think this is better performance-wise than creating an additional actor.
-JGary
What if you have an actor on the bottom of the screen, on the X axis, and you only want that actor to stay on the ground, and not move up or down, or float away, how would you do that?
If you can show screens like you did above too, that would be great!
I'm assuming you would use the constrain attribute, but I just wanted to clarify
The actor only moves left and right on the X axis, as if he's walking side to side on the ground
Thanks Quantum!
I haven't been in the forums much in the past week, so if this has been mentioned, please pardon my amnesia, and refer me to that place
-JGary
I haven't had problems with my actor floating yet, but I thought I better put this rule in place to prevent this possibility
Do you have another suggestion, or does this sound like the right rule to create?
-JGary