This site requires JavaScript, please enable it in your browser!
Greenfoot back
ZacBuzzsaw
ZacBuzzsaw wrote ...

2015/12/12

Changing Y value and moving bullets "up"

ZacBuzzsaw ZacBuzzsaw

2015/12/12

#
I'm trying to make a Space Invaders scenario and I'm having trouble getting the user's ship to fire bullets in the right direction. How do you change the Y value of an actor and not change the X value of an actor at the same time?
danpost danpost

2015/12/12

#
If the image of your bullets are facing right to begin with and you rotate them to face up, then the 'move' method will be sufficient to move the bullets. If they face upward to begin with, then use the 'setLocation' method using 'getX' for the x part and 'getY-speed' for the y part.
fejfo fejfo

2015/12/13

#
with other words use :
setLocation(getX(),<y>);
ZacBuzzsaw ZacBuzzsaw

2015/12/13

#
Is
fejfo wrote...
setLocation(getX(),<y>);
exactly how I write it, or is it just some variable with an unknown name that I need to create before the line compiles?
danpost danpost

2015/12/13

#
This part '<y>' is not valid code as written there (or, it is pseudo-code). It should be replaced with something like 'getY()-speed' where 'speed' is an int value respresenting how fast you want your actor to move up the screen.
ZacBuzzsaw ZacBuzzsaw

2015/12/14

#
Gotcha. So something like this?
this.setLocation(this.getX(), this.getY()-10);
fejfo fejfo

2015/12/14

#
yeah but the "this." is not nessesairy
ZacBuzzsaw ZacBuzzsaw

2015/12/15

#
Force of habit, sorry. Makes the code easier for me to understand.
danpost danpost

2015/12/15

#
ZacBuzzsaw wrote...
Force of habit, sorry. Makes the code easier for me to understand.
Do not be sorry. Actually, it discloses the fact that all methods are executed on one object or another. This point is good for beginners to be aware of.
You need to login to post a reply.