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

2019/2/27

how do i make a actor spawn an actor? like i used the "addObject()" but it said variable not found i have a class for it tho so idk

ttkaigler ttkaigler

2019/2/27

#
private GreenfootImage left = new GreenfootImage("left.png");
    private GreenfootImage right= new GreenfootImage("right.png");
    private int ySpeed;
    private int apexTimer;
    /**
     * Act - do whatever the FlappyBird wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("right") == true)
        {
            setLocation(getX() + 4, getY());
            setImage(right);
        }
        
        if (Greenfoot.isKeyDown("left") == true)
        {
            setLocation(getX() - 4, getY());
            setImage(left);
        }
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround) // in middle of jump
        {
            if (ySpeed == 0 && apexTimer > 0) apexTimer--; // run apex timer
            if (ySpeed == 0 && apexTimer > 0) return; // apex timer still running
            ySpeed+=3; // adds gravity effect
            setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) // has landed (reached ground level)
            {
                setLocation(getX(), groundLevel); // set on ground
                Greenfoot.getKey(); // clears any key pressed during jump
           }
        }
        else // on ground
        {
            if ("up".equals(Greenfoot.getKey())) // jump key detected
            {
                ySpeed = -15; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave ground
                apexTimer = 5;  // set apex timer (adjust value to suit)
            }
        }
        
        if (Greenfoot.isKeyDown("space") == true)
        {
           addObject(attack(), getX()+1,getY());
        }
    }    
}
danpost danpost

2019/2/27

#
ttkaigler wrote...
how do i make a actor spawn an actor? like i used the "addObject()" but it said variable not found i have a class for it tho so idk
To construct an object, you need to use the keyword new. The following creates an attack object and assigns it to a variable I called atak:
Actor atak = new attack();
If you then tried:
addObject(new attack(), getX()+1, getY());
you will get another error saying it cannot find the addObject method. This method can only be executed on a World object. Using the getWorld method, you have this:
getWorld().addObject(new attack(), getX()+1, getY());
ttkaigler ttkaigler

2019/2/27

#
oooooooh ok thank you very much. but also wouldnt i do an timer to despawn the attack?
if (Greenfoot.isKeyDown("space") == true)
        {
            getWorld().addObject(new attack(), getX()+1, getY());
            attackTimer+=2;
            if(attackTimer>=0)
            {
                 
            }
        }
danpost danpost

2019/2/28

#
ttkaigler wrote...
oooooooh ok thank you very much. but also wouldnt i do an timer to despawn the attack? << Code Omitted >>
The timer you refer to can be placed in the class of the actor itself.
You need to login to post a reply.