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

2
3
4
5
6
7
8
danpost danpost

2014/5/2

#
'selected' is a field name (not a value). You can say 'selected = such'n'such;', but you cannot say 'such'n'such = selected;'. When assigning a value to a field, the field is always on the left and the value, or expression determining the value, is always on the right. The class name in front of it in the declaration statement is similar to 'int' or 'boolean' in from of their field names. In fact, other than the primitive type (int, double, float, byte, boolean, char, long), variables (or fields), the only other type of variable is one of an Object (or subclass of Object) type. A 'String' is an example of an Object type. Variables of an Object type can be either 'null' (absent of an object) or contain an object that is of the field's declared type. The above, if you were to use it, which you will NOT, should be written this way:
if (selected == this && Greenfoot.isKeyDown("w"))
{
    setLocation(getX(), getY()-1);
    selected = null;
}
'this' is a keyword that refers to the object being executed or acted on. If in your subclass of World, it would refer to the world instance that is being 'acted' on. If in an Actor subclass, it would refer an instance of that class type (an instance of a subclass is still an instance of that class). When running, greenfoot will execute the active world's act method and then execute the act method of all the actors within that world. 'this' would refer to the actor or world whose act method is being executed on. Lesson over. The act method of your Tile class should be something like this:
public void act()
{
    if (selected != this) return;
    int dx = 0, dy = 0;
    String key = Greenfoot.getKey();
    if (getY() > 0 && "up".equals(key)) dy--;
    if (getY() < getWorld().getHeight()-1 && "down".equals(key)) dy++;
    if (getX() > 0 && "left".equals(key)) dx--;
    if (getX() < getWorld().getWidth()-1 && "right".equals(key)) dx++;
    if (dx == 0 && dy == 0) return;
    ((Bejeweled_World)getWorld()).moveSelected(actor, dx, dy);
}
I think 'getKey' is better for what you need here. Next, the 'moveSelected' method will need to be written into your Bejeweled_World class to process the move or refute it if no matches are present after the swapping.
Zzimon Zzimon

2014/5/2

#
the 'moveSelected' you're talking about is only to determine whether the move is correct or not, with the Tile class act method you provided the tile that i select should move if I click a tile and then click up, right?
Zzimon Zzimon

2014/5/2

#
The code in my Tile class currently is this:
public class Tile extends greenfoot.Actor  
{
    static Tile tileSelected;
    public Tile()  
    {  
        getImage().scale(40, 40);  
    }
    public static void setSelected(Tile tile)  
    {  
        tileSelected = tile;  
    }
    public void act()  
    {  
        if (selected != this) return;  
        int dx = 0, dy = 0;  
        String key = Greenfoot.getKey();  
        if (getY() > 0 && "up".equals(key)) dy--;  
        if (getY() < getWorld().getHeight()-1 && "down".equals(key)) dy++;  
        if (getX() > 0 && "left".equals(key)) dx--;  
        if (getX() < getWorld().getWidth()-1 && "right".equals(key)) dx++;  
        if (dx == 0 && dy == 0) return;  
        ((Bejeweled_World)getWorld()).moveSelected(actor, dx, dy);  
    }  
}  
but now I'm getting an error saying 'cannot find symbol - variable selected'
danpost danpost

2014/5/2

#
No. We are completely done in the Tile class at this point. Everything else is done in the 'moveSelected' method (or methods it might call) in the Bejeweled_World class. The method should (1) swap the tiles (2) determine legality of move * if legal, (a) perform match removals (b) set selected tile back to null * if not legal (a) un-swap the tiles
danpost danpost

2014/5/2

#
Sorry, I named the field 'tileSelected', not 'selected'. Change line 14 in your last post to:
if (tileSelected != this) return;
Zzimon Zzimon

2014/5/2

#
Thanks, and to clarify this
Zzimon wrote...
the 'moveSelected' you're talking about is only to determine whether the move is correct or not, with the Tile class act method you provided the tile that i select should move if I click a tile and then click up, right?
was more meant as when we move on to the moveSelected I should be able to move my tiles just through the act code provided. Without any checks or the like of course.
Zzimon Zzimon

2014/5/2

#
Now it's saying 'cannot find symbol - variable actor' in regards to line 22 of my previous post
danpost danpost

2014/5/2

#
Zzimon wrote...
Now it's saying 'cannot find symbol - variable actor' in regards to line 22 of my previous post
That 'actor' should be 'this'. (I was getting ahead of myself)
Zzimon Zzimon

2014/5/2

#
Thanks, though it seems there's another one ^^'. Again in line 22 it says 'cannot find symbol - method moveSelected(Tile,int,int)'
danpost danpost

2014/5/2

#
You have not added that method ... yet! You can test it first by adding this method into your Bejeweled_World class code:
public void moveSelected(Tile tile, int dx, int dy)
{
    makeSwap(tile, dx, dy);
}

private void makeSwap(Tile tile, int dx, int dy)
{
    int x = tile.getX();
    int y = tile.getY();
    Actor tile2 = (Actor)getObjectsAt(x+dx, y+dy, Tile.class).get(0);
    tile.setLocation(x+dx,y+dy);
    tile2.setLocation(x, y);
}
I added a separate method for the actual swapping because you may need to perform this action again if the move was not legal.
Zzimon Zzimon

2014/5/2

#
using this should my tiles be swappable/movable?
danpost danpost

2014/5/2

#
Try it and see if you can swap a few tiles around.
Zzimon Zzimon

2014/5/2

#
It works! :D they swap and everything! Thanks :D
Zzimon Zzimon

2014/5/2

#
Ok so now we just need to polish up this function so illegal moves aren't possible :) Damn it feels good to be this close.
danpost danpost

2014/5/2

#
Zzimon wrote...
Damn it feels good to be this close.
Just when you think you are almost there! :+?
There are more replies on the next page.
2
3
4
5
6
7
8