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

2014/5/1

Selecting characters and switching them with others

1
2
3
4
5
6
Zzimon Zzimon

2014/5/2

#
Will this work then? in regards to the
danpost wrote...
Change 'minorPIP' at all places with 'pip'.
public class Major extends World
{
    PIP pip;
    PIP pip2;
    boolean wDown;
    boolean sDown;
    Actor mouseActor;
    int mouseOffX, mouseOffY;
    public Major()
    {    
        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);
    }
    public void act()
    {
        // control running state the wombat PIP objects
        if (!wDown && Greenfoot.isKeyDown("1"))
        {
            pip.setActiveState(!pip.getActiveState());
            wDown = true;
        }
        if (wDown && !Greenfoot.isKeyDown("1")) wDown = false;
        // control running state the ship PIP objects
        if (!sDown && Greenfoot.isKeyDown("2"))
        {
            pip2.setActiveState(!pip2.getActiveState());
            sDown = true;
        }
        if (sDown && !Greenfoot.isKeyDown("2")) sDown = false;
        if (pip != null && pip.getWorld() != null && Greenfoot.mouseClicked(pip))  
        {  
            MouseInfo mouse = Greenfoot.getMouseInfo();  
            if (mouse == null) return;  
            int x = (mouse.getX()-(pip.getX()-pip.getWidth()/2))/minor.getCellSize();  
            int y = (mouse.getY()-(pip.getY()-pip.getHeight()/2))/minor.getCellSize();  
            Tile.setSelected((Tile)minor.getObjectsAt(x, y, Tile.class).get(0));  
        }  
    }
}
with this code I'm getting the error 'cannot find symbol - method getWidth()'
danpost danpost

2014/5/2

#
I guess I had you change it to the wrong thing. Here is what I think you should have in your Major class right now:
import greenfoot.*;

public class Major extends World
{
    PIP pip;
    World minor;

    public Major()
    {    
        super(1675, 910, 1); 
        //paint the background blue
        GreenfootImage background = getBackground();
        background.setColor(Color.blue);
        background.fill();
        //spawn the Bejeweled/puzzle world
        minor = new Bejeweled_World();
        pip = new PIP(minor, order);
        addObject(pip, 1070, 605);
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(pip))  
        {  
            MouseInfo mouse = Greenfoot.getMouseInfo();  
            if (mouse == null) return;  
            int x = (mouse.getX()-(pip.getX()-minor.getWidth()/2))/minor.getCellSize();  
            int y = (mouse.getY()-(pip.getY()-minor.getHeight()/2))/minor.getCellSize();  
            Tile.setSelected((Tile)minor.getObjectsAt(x, y, Tile.class).get(0));  
        }  
    }
}
Zzimon Zzimon

2014/5/2

#
With this my PIP actors activation won't work will it? Pressing '1' at the beginning is a minor detail that I can easily live with if it doesn't impact the other code
danpost danpost

2014/5/2

#
Just insert at line 19 (of my code) the following line:
pip.setActiveState(true);
Zzimon Zzimon

2014/5/2

#
EDIT: irrelevant comment posted before seeing your post again ^^'
Zzimon Zzimon

2014/5/2

#
Ok thanks, well now it is active all the time, now I just need to fix movement. Another thing I don't understand, with your code my world got significantly smaller and now it doesn't even seem like the Bejeweled part is a PIP object, don't know why. now it looks like this: instead of it earlier being in the bottom right corner of a blue background
danpost danpost

2014/5/2

#
I did not change the size of any images; and nothing I did should have altered any of them. So, I cannot say why this happened.
Zzimon Zzimon

2014/5/2

#
That's the thing I can't see how it could've possibly changed either. ok I'll try listing the different actors if that'd help Major
public class Major extends World
{
    PIP pip;
    World minor;
    public Major()
    {    
        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.setActiveState(true);
    }
    public void act()
    {
        if (Greenfoot.mouseClicked(pip))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (mouse == null) return;
            int x = (mouse.getX()-(pip.getX()-minor.getWidth()/2))/minor.getCellSize();
            int y = (mouse.getY()-(pip.getY()-minor.getHeight()/2))/minor.getCellSize();
            Tile.setSelected((Tile)minor.getObjectsAt(x, y, Tile.class).get(0));
        }
    }
}
Bejeweled_World
public class Bejeweled_World extends World
{
    public Bejeweled_World()
    { 
        super(20, 10, 60); 
        GreenfootImage bg = new GreenfootImage("Bejeweled_bg.jpg");
        bg.scale(getCellSize(), getCellSize());
        setBackground(bg);
        prepare();
    }
    private boolean canPlace(Actor actor, int x, int y)
    {  
        if (actor == null) return false;  
        if (y > 1 && getObjectsAt(x, y-2, Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass())  
        { return false;} 
        if (x > 1 && getObjectsAt(x-2, y, Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x-1, y, Actor.class).get(0).getClass() == actor.getClass())
        {return false;}
        else
        {
         return true;
        }
    }
    public void prepare()
    {
        for (int row=0; row<getHeight(); row++) for (int col=0; col<getWidth(); col++)
        {
            Actor actor = null;
            while (!canPlace(actor, col, row))
            {
                int which = Greenfoot.getRandomNumber(3);
                if(which == 0) actor = new Sword();
                if(which == 1) actor = new Coin();
                if(which == 2) actor = new Torch();
            }
            addObject(actor, col, row);
        }
    }
}
PIP
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;
        }
    }
}
Zzimon Zzimon

2014/5/2

#
I'm thinking it might be because of this:
public class Major extends World
{
    PIP pip;
    World minor;
    public Major()
    {    
        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.setActiveState(true);
    }
in line 4 it calls the World actor minor right and then it makes it equal to a new Bejeweled_World in line 13, after which it says the pip actor is equal to a new PIP which is the World actor called minor. That, is the only thing that makes sense in my head at the moment, though how to fix it escapes me.
danpost danpost

2014/5/2

#
Line 13 should not the 'World' at the beginning of it. It should be:
minor = new Bejeweled_World();
Zzimon Zzimon

2014/5/2

#
Hmm didn't really change much ^^', nothing that I can see at least :/
Zzimon Zzimon

2014/5/2

#
could it be the 'World minor;' line? can't really see what else could be causing it
danpost danpost

2014/5/2

#
No. If the size of the image of the PIP actor changed, it has to be either a 'pip.getImage().scale' was called or you changed the dimensions of the Bejeweled_World world. I cannot think of any other way it could be changed. You may have to upload the project with source so I could take a look at it.
Zzimon Zzimon

2014/5/2

#
ok I'll go ahead and try that
Zzimon Zzimon

2014/5/2

#
Here's the link: Bejeweled
There are more replies on the next page.
1
2
3
4
5
6