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

9
10
11
12
13
Zzimon Zzimon

2014/5/4

#
Ok, done, though I still get the error message 'variable tile is already defined in method moveSelected(Tile,int,int)' now it's just from the second for statement.
public void moveSelected(Tile tile, int dx, int dy)  
    {  
        Tile tile2 = makeSwap(tile, dx, dy); 
        if (matchingSetFound(tile) || matchingSetFound(tile2))
        {
            Tile.setSelected(null);  
            List<Tile> tiles = new ArrayList();  
            tiles.addAll(getHorizontalMatchingTiles(tile.getX(), tile.getY()));  
            tiles.addAll(getVerticalMatchingTiles(tile.getX(), tile.getY()));  
            tiles.addAll(getHorizontalMatchingTiles(tile2.getX(), tile2.getY()));  
            tiles.addAll(getVerticalMatchingTiles(tile2.getX(), tile2.getY())); 
            for (int n=0; n<tiles.size(); n++)
            {
                Tile tile = tiles.get(n);
                Tile dummy = new Dummy();
                addObject(dummy, tile.getX(), tile.getY());
                removeObject(tile);
                tiles.set(n, dummy);
            }
            for (Tile tile : tiles)  
            {  
                int x = tile.getX(), y = tile.getY();  
                Tile actor = null;  
                while (!canReplace(actor, x, y)) actor = getRandomTile();  
                addObject(actor, x, y);  
            }  
        }else{
            makeSwap(tile2, dx, dy);
        }
    }  
danpost danpost

2014/5/4

#
Ok. Change 'tile' throughout that 'for' block with 'tile3' (lines 20 and 22).
Zzimon Zzimon

2014/5/4

#
that fixed those, now i get 'cannot find symbol method getRandomTile()
danpost danpost

2014/5/4

#
Zzimon wrote...
that fixed those, now i get 'cannot find symbol method getRandomTile()
Change it to 'getRandomActor'.
Zzimon Zzimon

2014/5/4

#
'incompatible types', marking the 2 parentheses behind 'getRandomActor'
danpost danpost

2014/5/4

#
Change it to '(Tile)getRandomActor'.
Zzimon Zzimon

2014/5/4

#
ok, Awesome, no errors now :)
danpost danpost

2014/5/4

#
Only thing left. Remove the dummies.
Zzimon Zzimon

2014/5/4

#
Don't we still need to change it into another Tile actor? EDIT: to what I understand through the 'for(Tile tile3 : tiles)' the Dummy Tile actors should be adding a random other tile on top of themselves so I guess it's just the removal of the dummies
danpost danpost

2014/5/4

#
Zzimon wrote...
Don't we still need to change it into another Tile actor?
The first for loop (after creating the list) changes the Tiles from the matching class(es) to Dummy type. The second for loop adds a new random Tile at each dummy location. We just need to remove the dummies.
Zzimon Zzimon

2014/5/4

#
It creates new dummies every time a match is made right? if that's the case can't we just use a 'getWorld.removeObject(Dummy)' ?
danpost danpost

2014/5/4

#
Zzimon wrote...
It creates new dummies every time a match is made right? if that's the case can't we just use a 'getWorld.removeObject(Dummy)' ?
You could, but why create a list you already have. Just use:
removeObjects(tiles);
Also, if you were to do it in your suggested way, it would be:
removeObjects(getObjects(Dummy.class));
With your suggestion, here are the problems: (1) It is coded in the World subclass so 'getWorld' is not used -- it is an Actor class method (2) 'removeObject' will only remove one actor, not a list of actors; and, (3) 'removeObject' takes an actor for the argument, not a class.
Zzimon Zzimon

2014/5/4

#
I just forgot to think about that ^^', so that goes after after the alst for loop like this:
public void moveSelected(Tile tile, int dx, int dy)  
    {  
        Tile tile2 = makeSwap(tile, dx, dy); 
        if (matchingSetFound(tile) || matchingSetFound(tile2))
        {
            Tile.setSelected(null);  
            List<Tile> tiles = new ArrayList();  
            tiles.addAll(getHorizontalMatchingTiles(tile.getX(), tile.getY()));  
            tiles.addAll(getVerticalMatchingTiles(tile.getX(), tile.getY()));  
            tiles.addAll(getHorizontalMatchingTiles(tile2.getX(), tile2.getY()));  
            tiles.addAll(getVerticalMatchingTiles(tile2.getX(), tile2.getY())); 
            for (int n=0; n<tiles.size(); n++)
            {
                tile = tiles.get(n);
                Tile dummy = new Dummy();
                addObject(dummy, tile.getX(), tile.getY());
                removeObject(tile);
                tiles.set(n, dummy);
            }
            for (Tile tile3 : tiles)  
            {  
                int x = tile3.getX(), y = tile3.getY();  
                Tile actor = null;  
                while (!canReplace(actor, x, y)) actor = (Tile)getRandomActor();  
                addObject(actor, x, y);  
            }  
            removeObjects(tiles);
        }else{
            makeSwap(tile2, dx, dy);
        }
    } 
right?
danpost danpost

2014/5/4

#
Zzimon wrote...
I just forgot to think about that ^^', so that goes after after the alst for loop like this: < Code Omitted > right?
Precisely. If any more errors, let me know (I am sure you will, if there are any).
Zzimon Zzimon

2014/5/4

#
I get this message if I try to move anything. java.lang.NullPointerException at Bejeweled_World.canReplace(Bejeweled_World.java:83) at Bejeweled_World.moveSelected(Bejeweled_World.java:73) at Tile.act(Tile.java:30) at PIP.act(PIP.java:46) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) After it pops up and pauses the game, if I run it again the game just removes the Tiles e.g. 3 swords, and replaces them with Dummy actors.
There are more replies on the next page.
9
10
11
12
13