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

2017/3/31

I need to make an actor run in a track in another world other than the origanal.

1
2
fish fish

2017/3/31

#
here is the code. Player1 boolean click = false; String temp = Greenfoot.getKey(); if(click==false && Greenfoot.isKeyDown("s")) { move(5); click=true; } if(click==true && !Greenfoot.isKeyDown("s")) { click=false; } GreenfootImage image = getImage(); image.scale(50, 60); if(isTouching(finishline.class)) { getWorld().showText("Player 1 Won!",300,200); } } Player2 boolean click = false; String temp = Greenfoot.getKey(); if(click==false && Greenfoot.isKeyDown("k")) { move(5); click=true; } if(click==true && !Greenfoot.isKeyDown("k")) { click=false; } GreenfootImage image = getImage(); image.scale(50, 60); if(isTouching(finishline.class)) { getWorld().showText("Player 2 Won!",300,200); } } } Button if (Greenfoot.mouseClicked(this)) { Greenfoot.setWorld(new MyWorld()); } }
Nosson1459 Nosson1459

2017/3/31

#
I don't understand what's going on here.
fish wrote...
I need to make an actor run in a track in another world other than the origanal.
In my opinion you should read this.
fish fish

2017/4/1

#
So, the object of the game is in two parts. The first level is kind of like a 100m race. When ever race is finished you would press on the finish line. From there it would show a whole track. So this is where the problem comes. I need both actors to run around the track. So, this game includes two users and one user would press the "s" key and the other presses the "k" key. Keep in mind the second level is in another world.But the problem is that the actors only go straight they need to be able to go around the track.
Super_Hippo Super_Hippo

2017/4/1

#
At least two options: - actor checks the background image to follow a line - actor follows a path of fixed positions saved in an array If the track is for example a circle or a square, this can obviously be simplified and doesn't require something too complicated.
fish fish

2017/4/1

#
The track is a oval shape.
fish fish

2017/4/1

#
The track looks like this.
Super_Hippo Super_Hippo

2017/4/1

#
This is actually no oval, but two half circles which are connected with a straight part. I wrote the code for this scenario some time ago. Wanted to do more with it, but didn't do it yet. Well, I just uploaded it, so maybe it helps to understand how the following of a line works. My traffic simulation scenarios were made before this and there I let the cars follow a path of given coordinates.
fish fish

2017/4/1

#
ok, Thank you!
fish fish

2017/4/1

#
So, I am a beginner programmer and I don't understand most of this.
fish fish

2017/4/1

#
So, then how would I make the actor follow a path of fixed positions saved in an array?
Super_Hippo Super_Hippo

2017/4/1

#
Okay, I can't copy that from my traffic simulations because this code is far too much. Basically, you save coordinates of the points where the actor should run.
private static final int[] routeX = {100, 200, 250, 200, 100, 50},
                           routeY = {50, 50, 100, 150, 150, 100};
The first one is for the x-coordinate and the second for the y-coordinate, so in the example there are the points (100|50), (200|50), (250|100) and so on. Then, your actor needs to keep track where he is, so he knows which point he is running for:
private int position = 0;
To follow, it will look like this:
//move straight
//if the distance to the next point is smaller than its speed
    //increase the position variable
    //turn to next point
You may need to import the SmoothMover class to make this work. In your case, it might work to have something else though:
private int position = 0;
private boolean turning = false;

//whenever a click is activated
position++;
if (position % 36 == 0) turning = !turning;
if (turning) turn(5);
move(2);
You might have to adjust the values a bit.
fish fish

2017/4/1

#
Thank you
fish fish

2017/4/2

#
I just put in the code like this but nothings happening. The actors keep on going straight on. When its supposed to go in around like an oval. I also used the other way to and it didn't work either.
 public void addedToWorld(RWorld RWorld)

    {

        //whenever a click is activated
        position++;
        if (position % 36 == 0) turning = !turning;
        if (turning) turn(5);

        move(2);
    }
danpost danpost

2017/4/2

#
I am quite sure that Hippo wanted you to put that code in the act method -- not the addedToWorld method.
fish fish

2017/4/2

#
But this needs to happen in a different world than the original one.
There are more replies on the next page.
1
2