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

2015/4/27

_#How to make actor move left & right like space invaders

neesan neesan

2015/4/27

#
what i basically want the actors to do , in this case "aliens" is move in constant motion shifting from left to right just like in space invaders , i have been working on it for quite some time now with no breakthroughs , in dire need of help , much appreciated :)
azazeel azazeel

2015/4/27

#
source code please...
Super_Hippo Super_Hippo

2015/4/27

#
1
2
3
4
5
6
7
8
public void act()
{
    int dx = 0;
    if (Greenfoot.isKeyDown("right")) dx+=2;
    if (Greenfoot.isKeyDown("left")) dx-=2;
    if (dx!=0) setLocation(getX()+dx, getY());
    if (getX()<5 || getX()>getWorld().getWidth()-6) setLocation(getX()-dx, getY());
}
danpost danpost

2015/4/27

#
You mean movement like as shown in this demo?
xWanna xWanna

2015/4/27

#
You have to read out the coordinates first.
1
2
3
4
5
6
7
8
int x = getX();
int y = getY();
 
if(Greenfoot.isKeyDown("left"){
setLocation(x-2, y)
}else if(Greenfoot.isKeyDown("right"){
setLocation(x+2, y)
}
And so on
You need to login to post a reply.