Page 1 of 1

Ship flies off screen

PostPosted: Mon Jul 05, 2010 1:29 am
by ESL
I made an actor event by doing Key Down, assigning a key, and then the script
xvelocity = x + 10;

I did this to make it go up, down,left and right but the ship flies off the screen. How do I keep it on the screen?

Re: Ship flies off screen

PostPosted: Mon Jul 05, 2010 1:59 am
by krenisis
Ok lets make your ship go left right up and down

to make your ship go left put this code in script editor
x-=10;
to make your ship go right
x+=10;
to make you go up
y+=10;
to make your ship go downn
y-=10;

Any more questions just ask.

Re: Ship flies off screen

PostPosted: Mon Jul 05, 2010 4:42 am
by DST
You will find there are several ways to do anything in programming, the question is which one suits you best.

One way is to cap the player position inside screen boundaries by comparing x and y to width and height;
(you can use view.width and view.height, it's the same as your game resolution)

Player>Draw actor>
Code: Select all
if(xscreen>view.width){
xscreen=view.width;
}


xscreen refers to the onscreen position, while x refers to absolute game position.
The downside to this method is that you have to add it for all the directions, but that should be simple enough...
one for xscreen<0, yscreen<0, xscreen>view.width, and yscreen >view.height.

If you wanted the player to stay completely onscreen, add or subtract 1/2 the player width.

Code: Select all
if(xscreen>view.width-(width/2)){
xscreen=view.width-(width/2);}
else if(xscreen<(width/2)){
xscreen=(width/2);}


Here, width by itself refers to the width of the actor the script is called in; in this case, your player. Any actor variable called without a reference (like, x, y, width, height, angle) is always assumed to refer to the owner of the script.

Re: Ship flies off screen

PostPosted: Tue Jul 06, 2010 2:06 am
by ESL
Thanks! It is fairly complicated for me but your tips help a lot.

Re: Ship flies off screen

PostPosted: Tue Jul 13, 2010 9:52 am
by lcl
I thought to tell you what went wrong with your own script, so you can learn about it. :D
ESL wrote:I made an actor event by doing Key Down, assigning a key, and then the script
xvelocity = x + 10;
I did this to make it go up, down,left and right but the ship flies off the screen. How do I keep it on the screen?

The problem was that you had written xvelocity. xvelocity is a value that makes it slide on x axis, and it won't stop sliding though you'll release the key,
'cause your code sets the xvelocity, it will stay.
So, you'd better use:
Code: Select all
x=x+10;

or shorter code:
Code: Select all
x+=10;

Both of those codes increase the value of x with 10.
There is also yvelocity. It's similar to xvelocity unless it makes actor slide on y axis.

I hope that you now understand xvelocity and yvelocity, it makes all easier. :D

- lcl -

Re: Ship flies off screen

PostPosted: Wed Jul 14, 2010 12:15 am
by ESL
Actually, it does make sense. Thanks for your help.