Ship flies off screen

Game Editor comments and discussion.

Ship flies off screen

Postby ESL » Mon Jul 05, 2010 1:29 am

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?
ESL
 
Posts: 96
Joined: Mon Jun 28, 2010 2:05 am
Score: 1 Give a positive score

Re: Ship flies off screen

Postby krenisis » Mon Jul 05, 2010 1:59 am

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.
Tutorial Database for all beginners click this link
viewtopic.php?f=4&t=8680
krenisis
 
Posts: 606
Joined: Sat Jul 25, 2009 3:27 pm
Score: 51 Give a positive score

Re: Ship flies off screen

Postby DST » Mon Jul 05, 2010 4:42 am

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.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Ship flies off screen

Postby ESL » Tue Jul 06, 2010 2:06 am

Thanks! It is fairly complicated for me but your tips help a lot.
ESL
 
Posts: 96
Joined: Mon Jun 28, 2010 2:05 am
Score: 1 Give a positive score

Re: Ship flies off screen

Postby lcl » Tue Jul 13, 2010 9:52 am

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 -
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Ship flies off screen

Postby ESL » Wed Jul 14, 2010 12:15 am

Actually, it does make sense. Thanks for your help.
ESL
 
Posts: 96
Joined: Mon Jun 28, 2010 2:05 am
Score: 1 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron