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

2018/9/5

Back and forth movement

1
2
DavidTshitenda DavidTshitenda

2018/9/5

#
Hi I have a space fighter game where the protector would shoot through asteroids to defeat the enemy ships. My problem is I want to create a floating/vibrating effect for the asteroid movement where they move back and forth just by for example 5px.
Super_Hippo Super_Hippo

2018/9/6

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private int xMiddle;
private int direction = Greenfoot.getRandomNumber(2)*2-1; //+1 or -1
private int speed = 1; //only needed if something else than 1
 
public void addedToWorld(World w)
{
    xMiddle = getX();
}
 
public void act()
{
    setLocation(getX()+direction*speed);
    if (getX()<xMiddle-4 || getX()>xMiddle+4) direction *= -1; //change direction of movement
}
DavidTshitenda DavidTshitenda

2018/9/6

#
Sorry for omitting code as I didn’t have any. The setLocation doesn’t have a y value and I am getting a error.
DavidTshitenda DavidTshitenda

2018/9/6

#
The asteroid are all moving in different directions. If I was clear earlier I am sorry so basically I want all the asteroids to move in the same direction, so it’s 5 pixels forward and the 5 pixels back and this action should be repeated until the game is complete.
DavidTshitenda DavidTshitenda

2018/9/6

#
So in essence I want the actor to move 5 pixels in the y axis then back to its original position using maybe a loop.
DavidTshitenda DavidTshitenda

2018/9/6

#
The above code separates the asteroids
Super_Hippo Super_Hippo

2018/9/6

#
Yes, I forgot to include to y-value. Yes, you were unclear. Maybe this is better?
1
2
3
4
5
6
7
8
9
10
11
12
private static int direction = Greenfoot.getRandomNumber(2)*2-1;
private int speed = 1;
  
public void act()
{
    setLocation(getX()+direction*speed, getY());
}
 
public static void changeDirection()
{
    direction *= -1;
}
1
2
3
4
5
6
7
8
9
10
11
//in world
private int directionTimer = 5;
 
public void act()
{
    if (--directionTimer==0)
    {
        Asteroid.changeDirection();
        directionTimer=10;
    }
}
Not sure why I wanted to save the x-value.
DavidTshitenda DavidTshitenda

2018/9/6

#
The above code still separates my asteroids, if this makes sense all I want to do is move it forward (move(5)) and then back (move(-5)) but this movement is looped. If think about it its the same as saying if it’s at the top of the world move to the bottom of the world then back with this whole movement looped. But what I want to do is restrict it so that it doesn’t touch the top of the world it only goes a few pixels forward then back then forward again with this whole movement being looped. I will screenshot what I am talking about next sorry for all that
Super_Hippo Super_Hippo

2018/9/6

#
They should move in sync with the code I posted. Could you show how you tried to implement it? And show what exactly happens?
DavidTshitenda DavidTshitenda

2018/9/6

#
I have uploaded my scenario to my profile you can check it out
DavidTshitenda DavidTshitenda

2018/9/6

#
The scenario I uploaded does not do what I am asking it does the bit of code you gave me but I will upload the scenario with the action I am trying to perform in a bit.
Super_Hippo Super_Hippo

2018/9/7

#
You forgot to add the 'static' to the direction-variable.
danpost danpost

2018/9/7

#
You could do something like this:
1
2
3
4
5
6
7
8
9
private int dirY = 1;
private int distY = 0;
 
public void act()
{
    setLocation(getX(), getY()+dirY);
    distY = (distY+1)%5;
    if (distY == 0) dirY = -dirY;
}
DavidTshitenda DavidTshitenda

2018/9/7

#
Super_hippo if I do that static thing then I get a “non static variable cannot be referenced from a static context” error.
DavidTshitenda DavidTshitenda

2018/9/7

#
Thanks danpost it works
There are more replies on the next page.
1
2