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

2014/5/1

Selecting an actor using Greenfoot.mousePressed(this)

Zzimon Zzimon

2014/5/1

#
Trying this again, what I'm trying to do is select an actor after clicking it so that when i have clicked an actor i can press w,a,s or d to get my actor to move either up, down, left or right and after the actor have moved for it to become un-selected again. I have tried this a couple of different ways and haven't been able to get any of them to work, actually if I could just get one tile to move in a direction when i click on it i would be thrilled but I can't. here's a picture of what my world looks like: The blue background is made from me using a class that creates a mini-world withing a world so as to get the grid working. I suspect that there is something in my PIP actor or my world actor that is blocking the mousePressed on each of the tile actors and making it click on the PIP element instead and I can't quite figure out which thing in my code that might be causing this. World actor:
import greenfoot.*; 
import java.awt.Color;
public class Main_Control extends World
{
    PIP pip;
    PIP pip2;
    boolean wDown;
    boolean sDown;
    Actor mouseActor;
    int mouseOffX, mouseOffY;
    
    public Main_Control()
    {    
        super(1675, 910, 1); 
        //paint the background blue
        GreenfootImage background = getBackground();
        background.setColor(Color.blue);
        background.fill();
        //spawn the Bejeweled/puzzle world
        World minor = new Bejeweled_World();
        Class[] order = { Sword.class };
        Class[] order2 = { Torch.class };
        Class[] order3 = { Coin.class };
        pip = new PIP(minor, order);
        addObject(pip, 1070, 605);
    }
}
PIP actor:
import greenfoot.*; 
import java.awt.Color;
public class PIP extends Actor
{
    private World minor;
    private Class[] paintOrder;
    private boolean activeState;
    Sensor sensor = new Sensor();
    int minorWidth, minorHeight, minorCellSize;
    /**
     * creates the PIP object
     */
    public PIP(World minorWorld, Class[] classes) 
    {
        minor = minorWorld;
        paintOrder = classes;
        minorCellSize = minor.getCellSize();
        minorWidth = minor.getWidth()*minorCellSize;
        minorHeight = minor.getHeight()*minorCellSize;
        sensor.setImage(new GreenfootImage(minorWidth+minorCellSize*2, minorHeight*minorCellSize*2));
        GreenfootImage image = new GreenfootImage(minorWidth+11, minorHeight+11);
        image.setColor(Color.white);
        image.fill();
        image.setColor(Color.black);
        image.drawRect(0, 0, minorWidth+10, minorHeight+10);
        image.drawRect(1, 1, minorWidth+8, minorHeight+8);
        image.drawRect(4, 4, minorWidth+2, minorHeight+2);
        setImage(image);
        updateImage();
    }    
    public void act()
    {
        if (!activeState) return;
        for (Object obj : minor.getObjects(null))
        {
            Actor actor = (Actor)obj;
            if (actor.getWorld() !=null) actor.act();
        }
        minor.act();
        updateImage();
    }
    private void updateImage()
    {
        GreenfootImage view = new GreenfootImage(minor.getBackground());
        for (Object obj : sensor.getIntersectors(null))
        {
            Actor actor= (Actor)obj;
            if(!isPaintOrderActor(actor))
            {
                int x = actor.getX()*minorCellSize+minorCellSize/2;
                int y = actor.getY()*minorCellSize+minorCellSize/2;
                GreenfootImage img = getActorImage(actor);
                view.drawImage(img, x-img.getWidth()/2, y-img.getHeight()/2);
            }
        }
        for(int i=1; i<=paintOrder.length; i++) for (Object obj: sensor.getIntersectors(paintOrder[paintOrder.length-i]))
        {
            Actor actor = (Actor)obj;
            int x = actor.getX()*minorCellSize+minorCellSize/2;
            int y = actor.getY()*minorCellSize+minorCellSize/2;
            GreenfootImage img = getActorImage(actor);
            view.drawImage(img, x-img.getWidth()/2, y-img.getHeight()/2);
        }
        getImage().drawImage(view, 5, 5);
    }
    private GreenfootImage getActorImage(Actor actor)
    {
        GreenfootImage actorImg = actor.getImage();
        int w = actorImg.getWidth();
        int h = actorImg.getHeight();
        int max = Math.max(w, h);
        GreenfootImage image = new GreenfootImage(max*2, max*2);
        image.drawImage(actorImg, max-actorImg.getWidth()/2, max-actorImg.getHeight()/2);
        image.rotate(actor.getRotation());
        return image;
    }
    private boolean isPaintOrderActor(Actor actor)
    {
        for(int i=0; i<paintOrder.length; i++) if(actor.getClass().equals(paintOrder[i])) return true;
        return false;
    }
    public void setActiveState(boolean newActiveState)
    {
        activeState = newActiveState;
    }
    public boolean getActiveState()
    {
        return activeState;
    }
    public void run()
    {
        setActiveState(true);
    }
    public void pause()
    {
        setActiveState(false);
    }
    public void step()
    {
        if (activeState) return;
        activeState = true;
        act();
        activeState = false;
    }
    private class Sensor extends Actor
    {
        public java.util.List getIntersectors(Class cls)
        {
            minor.addObject(this, minor.getWidth()/2, minor.getHeight()/2);
            java.util.List objects = getIntersectingObjects(cls);
            minor.removeObject(this);
            return objects;
        }
    }
}
and the tile actor with the selection/move script aka my Sword actor
import greenfoot.*; 
public class Sword extends Actor
{
    public Sword()
    {
        getImage().scale(40, 40);
    }
    private int state = 0;  
    public void act()  
    {  
        if (state == 0)
        {  
            if (Greenfoot.mousePressed(this)== true)
            {  
                state = 0+1;  
            }  
        }
        else if (state == 1)
        {  
            movement();  
        }  
    } 
    public void movement()
    {
        if (Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY() - 1);
            state = 0;
        }
    }
}
You need to login to post a reply.