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

2016/10/26

problem

Nosson1459 Nosson1459

2016/10/26

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Box here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Box extends Boxes
{
    private GreenfootImage image1=new GreenfootImage("mystery.png");
    private GreenfootImage image2=new GreenfootImage("MysteryG.png");
    private GreenfootImage image3=new GreenfootImage("Block.png");
    private int timer=0;
    private boolean hit=false;
    private int counter=0;
    private boolean empty=false;
    private boolean coin=false;
    private boolean shooting=false;
    public Box(int type)
    {
        if(type==1)
        {
            coin=true;
            shooting=false;
        }
        if(type==2)
        {
            shooting=true;
            coin=false;
        }
    }
    public Box()
    {
       setImage(image1);
    }
    /**
     * Act - do whatever the Box wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        if(!empty)
        {
            checkPengu();
            hit();
        }
        
        move();
        remove();
    }  
    private boolean checkPengu1()
    {
        Object under=getOneObjectAtOffset(0,getImage().getHeight()/2+1,Pengu.class);
        return under!=null;
    }
    private boolean checkPengu2()
    {
        Object under=getOneObjectAtOffset(getImage().getWidth()/2-1,getImage().getHeight()/2,Pengu.class);
        return under!=null;
    }
    private boolean checkPengu3()
    {
        Object under=getOneObjectAtOffset(-getImage().getWidth()/2+1,getImage().getHeight()/2,Pengu.class);
        return under!=null;
    }
    private void checkPengu()
    {
        if(checkPengu1()||checkPengu2()||checkPengu3())
        {
            hit=true;
        }
    }
    private void hit()
    {
        Scene scene=(Scene)getWorld();
        if(hit==true)
        {
            timer++;
            if(counter<6)
            {
                if(timer==2&&getImage()==image1)
                {
                    setImage(image2);
                    timer=0;
                    counter++;
                }
                if(timer==2&&getImage()==image2)
                {
                    setImage(image1);
                    timer=0;
                    counter++;
                }
            }
            
        }
        if(counter==5)
        {
            empty=true;
            if(shooting==true)
            {
                scene.addObject(new Shooting(),getX(),getY());
                scene.score=scene.score+100;
            }
            else if(coin==true)
            {
                scene.coins++;
                scene.score=scene.score+100;
            }
            counter=0;
            hit=false;
            setImage(image3);
        }
    }
    private void move()
    {
        Scene scene=(Scene)getWorld();
        if(Greenfoot.isKeyDown("Right")&&scene.move==true&&scene.onCloud==false)
        setLocation(getX()-7,getY());  
        if(scene.onCloud==true)
        {
            if(scene.way==true)
            {
                setLocation(getX()-4,getY());
            }
            if(scene.way==false)
            {
                setLocation(getX()+4,getY());
            }
        }
    }
}
for some reason when i use the int constructor
hit();
doesnt work
danpost danpost

2016/10/26

#
Nosson1459 wrote...
for some reason when i use the int constructor
hit();
doesnt work
Well, with good reason. Neither 'image1' nor 'image2' is set to the actor when you use that constructor.
Nosson1459 Nosson1459

2016/10/26

#
thnx
You need to login to post a reply.