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
Zzimon Zzimon

2014/5/2

#
Sorry I just realized I for some reason had only posted half of what I intended to write ^^'. The right sentence: All of my tile classes i.e. my Coin, Torch and Sword class just extend Actor, not a Tile class
danpost danpost

2014/5/2

#
You could simplify the code immensely by add a Tile class for these classes to extend.
Zzimon Zzimon

2014/5/2

#
Ok, I've now made a Tile actor though I still have the problem, even with the selected boolean script you provided that I cannot properly click the Sword tile because of the PIP actor since it has a function that makes it possible to move around the window while the game is running, this function I don't really need but the general code in that actor is quite a bit above my level so I can't really figure out which thing to remove to make it fixed. I have fixed the spawn so that my gridded world spawns in the right place so now I just need to remove whichever part it is that makes it moveable. EDIT: found the movement code, trying to fix it.
Zzimon Zzimon

2014/5/2

#
OK, I tried removing this part from my Main_Control world (my main world / background)
        if (Greenfoot.mousePressed(null) && mouseActor == null)
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (mouse.getActor() == null || mouse.getActor().getClass() != PIP.class) return;
            mouseActor = mouse.getActor();
            mouseOffX = mouseActor.getX()-mouse.getX();
            mouseOffY = mouseActor.getY()-mouse.getY();
            removeObject(mouseActor);
            addObject(mouseActor, mouse.getX()+mouseOffX, mouse.getY()+mouseOffY);
        }
        if (mouseActor != null && Greenfoot.mouseDragged(mouseActor))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            mouseActor.setLocation(mouse.getX()+mouseOffX, mouse.getY()+mouseOffY);
        }
        if (mouseActor != null && Greenfoot.mouseClicked(mouseActor)) mouseActor = null;
though my Sword tiles movement still doesn't seem to work
danpost danpost

2014/5/2

#
Oh. I forgot you were trying to make the Bejeweled_World a PIP actor world. You cannot use mouse clicks on the "actors" in a PIP actor image. Like the description of the PIP actor says, it shows the visual state of a non-active world; and my demo worlds show what you can do with it. It would take a lot to make it so that mouse actions would work within the PIP actor worlds. I think the easiest thing for you to do is to have your major world determine (1) if a click was performed on the Bejeweled_World PIP actor and (2) if so, where (on which tile), by using the location of the click with respect to the location of the PIP actor. Then, (3) inform the Bejeweled_World world which tile was selected. With that, we can program it so a selected tile will respond to a keystroke.
Zzimon Zzimon

2014/5/2

#
Awesome! A strategy, now I just need some code to determine how this will work ^^'
danpost danpost

2014/5/2

#
Okay. Before we start on implementing that, let us get your Tile class and its subclasses into shape. We will no longer need any mouse detection code in those classes and all three types of tiles have their images scaled to size. So, as strange as this may sound -- remove all code from the three Tile subclasses. For an example, they should all look like this:
public class Sword extends Tile{}
That is the entire class for Sword; and the Coin and Torch classes should look similar. Then, have this as your Tile class code:
public class Tile extends greenfoot.Actor
{
    static Tile tileSelected;

    public Tile()
    {
        getImage().scale(40, 40);
    }

    public static void setSelected(Tile tile)
    {
        tileSelected = tile;
    }
}
What is needed, now, is an act method in the Tile class to check for arrow keystrokes and inform the Bejeweled_World world when a move is selected. Now, back into the major world class. Its act method will need to capture mouse clicks on the BejeweledWorld PIP actor. I will call that PIP actor world 'minor' and the PIP actor for that world 'minorPIP'. The act method should have something like this within it:
if (minorPIP != null && minorPIP.getWorld() != null && Greenfoot.mouseClicked(minorPIP))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse == null) return;
    int x = (mouse.getX()-(minorPIP.getX()-minorPIP.getWidth()/2))/minor.getCellSize();
    int y = (mouse.getY()-(minorPIP.getY()-minorPIP.getHeight()/2))/minor.getCellSize();
    Tile.setSelected((Tile)minor.getObjectsAt(x, y, Tile.class).get(0));
}
I think that will work.
Zzimon Zzimon

2014/5/2

#
Ok, the code for my major class now looks like this:
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;
        // all the below if statements control dragging of PIP objects
        if (Greenfoot.mousePressed(null) && mouseActor == null)
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if (mouse.getActor() == null || mouse.getActor().getClass() != PIP.class) return;
            mouseActor = mouse.getActor();
            mouseOffX = mouseActor.getX()-mouse.getX();
            mouseOffY = mouseActor.getY()-mouse.getY();
            removeObject(mouseActor);
            addObject(mouseActor, mouse.getX()+mouseOffX, mouse.getY()+mouseOffY);
        }
        if (mouseActor != null && Greenfoot.mouseDragged(mouseActor))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            mouseActor.setLocation(mouse.getX()+mouseOffX, mouse.getY()+mouseOffY);
        }
        if (mouseActor != null && Greenfoot.mouseClicked(mouseActor)) mouseActor = null;
        if (minorPIP != null && minorPIP.getWorld() != null && Greenfoot.mouseClicked(minorPIP))  
        {  
            MouseInfo mouse = Greenfoot.getMouseInfo();  
            if (mouse == null) return;  
            int x = (mouse.getX()-(minorPIP.getX()-minorPIP.getWidth()/2))/minor.getCellSize();  
            int y = (mouse.getY()-(minorPIP.getY()-minorPIP.getHeight()/2))/minor.getCellSize();  
            Tile.setSelected((Tile)minor.getObjectsAt(x, y, Tile.class).get(0));  
        }  
    }
}
Although this woudn't work with the dragging of the PIP class in it would it? And for the minor thing to work would it be enough to just change the name of my Bejeweled_World to minor?
danpost danpost

2014/5/2

#
No. It will not work with the dragging of the PIP class Did you want to be able to drag the PIP objects as well?
Zzimon Zzimon

2014/5/2

#
No, the dragging is irrelevant as long as i have the spawn location It's fine, that way it's easier to make some layout too anyway.
danpost danpost

2014/5/2

#
Zzimon wrote...
No, the dragging is irrelevant as long as i have the spawn location It's fine, that way it's easier to make some layout too anyway.
Ok. Then remove everything from line 40 to line 56; change line 7 to the following:
Bejeweled_World minor;
and remove the first word from line 17 ( 'World' ).
Zzimon Zzimon

2014/5/2

#
EDIT: made this while my page hadn't refreshed so I didn't see your latest comment, will test that now. Can't really remove this because of lack of delete function
danpost danpost

2014/5/2

#
Are you going to be using more than one PIP actor in your project?
danpost danpost

2014/5/2

#
Change 'minorPIP' at all places with 'pip'.
Zzimon Zzimon

2014/5/2

#
I am thinking of in the final version to use 2 so that it will have a little 'adventure' part at the top of my game board, over my Bejeweled game so that they will act together so for example if I mix 3 sword tiles I will get a 'sword point' so I can damage monsters in the adventure mode but for now I'd just like to get my bejeweled game to function optimally without regard for any other PIP objects
There are more replies on the next page.
1
2
3
4
5