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

2014/1/17

turn toward

sanoy sanoy

2014/1/17

#
i have a problem. In one of my world there are 2 of the following actors.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class boctorok here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class boctorok extends Actor
{
    public static int direction;
    public int counter;
    /**
     * Act - do whatever the boctorok wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        bewegen();
    }
    
    public void bewegen()
    {
        if(getX() <58)
        {
            setLocation(getX()+3,getY());
        }
        if(getX() > 441)
        {
            setLocation(getX() - 3,getY());
        }
        if(getY() <230)
        {
            setLocation(getX(),getY()+3);
        }
        if(getY() > 446)
        {
            setLocation(getX() ,getY() - 3);
        }
        if(counter == 0)
        {
            direction = Greenfoot.getRandomNumber(4);
        }
        setRotation(direction * 90);
        if (counter < 50)
        {
            move(1);
        }
        counter = (counter + 1)%75;
        if(counter<75||counter>50)
        {
            if(Greenfoot.getRandomNumber(100)<5)
            {
                turn(90);
            }
            else
            if(Greenfoot.getRandomNumber(100)<5)
            {
                 getWorld().addObject(new shotRock2(),getX(),getY());
            }
        }
    }
}
In line 59 a new shotRock2 should be generated
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class shotRock2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class shotRock2 extends Actor
{
    public static int direction;
    /**
     * Act - do whatever the shotRock2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(1);
    }    
    
    public shotRock2()
    {
        
    }
}
This shotRock2 actor shold turn in the same direction the boctork, that generated the rock was facing at the moment of the generation(line 59) how could i do that.
Zamoht Zamoht

2014/1/17

#
Replace line 57-60 with the following:
            if(Greenfoot.getRandomNumber(100)<5)  
            {  
                 shotRock2 rock = new shotRock2();
                 getWorld().addObject(rock,getX(),getY());
                 rock.setRotation(getRotation());  
            } 
sanoy sanoy

2014/1/17

#
thx
sanoy sanoy

2014/1/17

#
if i have two b octoroks they are always facig the same direction. How could i make them move individually.
Dragonblazze Dragonblazze

2014/1/17

#
you would have to code a second constructor class of boctorok and if you are controlling them then you need to create two different methods of control but if they are by themselves you just need some random movement in (see Kolling's trick the turtle lessons)
You need to login to post a reply.