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

2017/11/1

Issue with changing images.

Beamo Beamo

2017/11/1

#
I am currently trying to make a short animation of an explosion that begins once the character comes in contact with the nuke class. However, when the nuke does collide with the character, everything in the scenario happening at that current moment stops, then continues after a second or two; without playing the animation. Here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class startenemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class startenemy extends Actor
{
    /**
     * Act - do whatever the startenemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    int timer = 20;
    int speed = 1;
    int imageTimer = 0;
    int span = 4;
    boolean exploded=false;
    public startenemy(){
        GreenfootImage idleOne = new GreenfootImage("enemyidle1.png");
        setImage(idleOne);
        exploded=false;
    }
    public void act(){
        if(exploded=true){moveTest();}
        if(getOneIntersectingObject(nuke.class)!=null){
            explode();
        }
    }    
    public void moveTest(){
        timer--;
        if (timer > 0) move(speed);
        if (timer == 0) speed = -2-Greenfoot.getRandomNumber(30); // max -2; min -31
        if (timer < 0 && timer == speed){
            speed = 1-2*Greenfoot.getRandomNumber(2); // 1 or -1
            timer = 2+Greenfoot.getRandomNumber(30); // min 2; max 31
        }
        if(speed<0){
            GreenfootImage opImage = new GreenfootImage("enemyidle1.png");
            opImage.mirrorHorizontally();
            setImage(opImage);
        }
        if(speed>0){
            GreenfootImage image = new GreenfootImage("enemyidle1.png");
            setImage(image);
        }
    }
    public void explode(){
        exploded = true;
        GreenfootImage explodeOne = new GreenfootImage("frame_1.png");
        GreenfootImage explodeTwo = new GreenfootImage("frame_2.png");
        GreenfootImage explodeThree = new GreenfootImage("frame_3.png");
        GreenfootImage explodeFour = new GreenfootImage("frame_4.png");
        GreenfootImage explodeFive = new GreenfootImage("frame_5.png");
        GreenfootImage explodeSix = new GreenfootImage("frame_6.png");
        GreenfootImage explodeSeven = new GreenfootImage("frame_7.png");
        GreenfootImage idleOne = new GreenfootImage("enemyidle1.png");
        imageTimer++;
        if(imageTimer==span){setImage(explodeOne);getImage().scale(idleOne.getHeight(),idleOne.getWidth());}
        if(imageTimer==span*2){setImage(explodeTwo);getImage().scale(idleOne.getHeight(),idleOne.getWidth());}
        if(imageTimer==span*3){setImage(explodeThree);getImage().scale(idleOne.getHeight(),idleOne.getWidth());}
        if(imageTimer==span*4){setImage(explodeFour);getImage().scale(idleOne.getHeight(),idleOne.getWidth());}
        if(imageTimer==span*5){setImage(explodeFive);getImage().scale(idleOne.getHeight(),idleOne.getWidth());}
        if(imageTimer==span*6){setImage(explodeSix);getImage().scale(idleOne.getHeight(),idleOne.getWidth());}
        if(imageTimer==span*7){setImage(explodeSeven);getImage().scale(idleOne.getHeight(),idleOne.getWidth());getWorld().removeObject(this);}
        nuke nuke = getWorld().getObjects(nuke.class).get(0);
        getWorld().removeObject(nuke);
    }
}
As usual, any help would be greatly appreciated! Thank you!
Beamo Beamo

2017/11/1

#
UPDATE: I've realised that I had forgotten to add a condition to the moveTest() function checking whether the boolean exploded had been set to true, after the explosion had been triggered. Because I hadn't incorporated this earlier, the image was still being set to the direction it was moving towards, cancelling the explosion animation. However, now I am encountering an error in the nuke class, in which the error states: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at nuke.follow(nuke.java:34) at nuke.act(nuke.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) (the "at nuke.follow(nuke.java:34), at nuke.act(nuke.java:20)" lines are highlighted) I assume this means that i cannot find an instance of the enemy class within the world. But in the code I have an if statement checking whether there is an instance of the enemy within the world, and I am unsure why I am getting this error. Here is the code for the nuke class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class nuke here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class nuke extends Actor
{
    int imageTimer=0;
    int span=2;
    public nuke(){
        GreenfootImage image = getImage();
        image.scale(image.getWidth()+50,image.getHeight()+50);
    }
    public void act() 
    {
        follow();
        move(5);
    }    
    private void follow(){
        if(getWorld().getObjects(startenemy.class)!=null){
            startenemy enemy = getWorld().getObjects(startenemy.class).get(0);
            int targetX = enemy.getX();
            int targetY = enemy.getY();
            turnTowards(targetX,targetY);
        }
    }         
}

Sorry for keeping this on the same thread, I can move it to another if necessary. Thanks for your time!
danpost danpost

2017/11/1

#
The prroblem is that line 23 will always be true. The 'getObjects' method always returns a List object. You need to check the size of the list (or its state of being empty) not whether it exists or not.
You need to login to post a reply.