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
Super_Hippo Super_Hippo

2017/4/2

#
The addedToWorld method is only executed once when the actor is added to the world. If you only want it to happen in the RWorld world, then you can have a condition in the act method.
public void act()
{
    if (getWorld() instanceof RWorld)
    {
        //...
    }
    else
    {
        //...
    }
}
danpost danpost

2017/4/2

#
fish wrote...
But this needs to happen in a different world than the original one.
Well, the addedToWorld method will only execute once when the actor is added into a world. If you need the actor to do something different in different world, you can use the 'instanceof' conditional to see what world the actor is in and behave appropriately for that type world:
if (getWorld() instanceof LWorld)
{
    // whatever
}
else if (getWorld() instance of RWorld)
{
   // whatever else
}
fish fish

2017/4/2

#
if (getWorld() instanceof RWorld)
        {
            if(click==false && Greenfoot.isKeyDown("k"))
            {
                move(5);
                click=true;
            }  
            if(click==true && !Greenfoot.isKeyDown("k"))
            {
                click=false;
            }    
           
            else  if (getWorld() instanceof RWorld)
            {
                position++;
                if (position % 40 == 0) turning = !turning;
                if (turning) turn(5); 
            }
        }
When I press compile the first level works fine. But when I get into the next level player two keeps on spinning and player one stays put. When the user presses the keys player one just goes straight off of the track and player two goes in circles off of the track. The above code is a snippet of the code from player two. How do I fix this problem?
Super_Hippo Super_Hippo

2017/4/2

#
I think there is a } missing in line 12. The 'else' in line 13 should execute if the condition in line 1 is not met.
fish fish

2017/4/2

#
It's still not working. I fixed the problem that you said and changed the values in line 16/17 but the actors still don't go around the whole track. http://www.bing.com/images/search?view=detailV2&ccid=iaBhq5wP&id=5BAFCDB043A0E9379FF3020157A97ADE08742A9B&q=Running+Track&simid=608042657565443821&selectedIndex=59&ajaxhist=0 The track looks something like this.
Super_Hippo Super_Hippo

2017/4/2

#
You also didn't add a move command for the RWorld. The idea about the values was that 36×5=180, so when turning, it turns 180 degrees in total.
fish fish

2017/4/2

#
Where do I have to add the move command? I Also tried other values but the actors didn't go all over the track. Plus the user is supposed to press the s button to move the actor around the track.
Super_Hippo Super_Hippo

2017/4/3

#
The move command should be added after line 17. It should move a similar track, but not the exact one. You have to adjust the values as needed. If I knew the exact shape of the track, one could calculate that, but it seems to be easier to just try to change the values until it fits to the track.
danpost danpost

2017/4/3

#
You might want to look at my Racetrack Demo scenario. It has three different car classes that use different codes for moving around the track -- one is line following; one is point instruction; and one it adjusted point instruction (the adjustment corrects the speeds around the turns to match the speed along the straightaways).
You need to login to post a reply.
1
2