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

2011/6/6

Is something like this possible?

I want to say that if the class "Ghost" contains the Image "picture1.png" the "Ghost"-class should move left and when the class contains the Image "picture2.png" it should move right somthing like that (I know it's wrong but i trief my best):
public void act()
{

if (setImage("picture1.png")&& !getOneObjectAtOffset( 4, 0, wall.class)&& !getOneObjectAtOffset (-4, 0, wall.class)&& getOneObjectAtOffset( 0, 4, wall.class)&& getOneObjectAtOffset( 0, -4, wall.class))

{
setLocation(getX()+4, getY());
}




if (setImage("picture1.png")&& !getOneObjectAtOffset( 4, 0, wall.class)&& !getOneObjectAtOffset (-4, 0, wall.class)&& getOneObjectAtOffset( 0, 4, wall.class)&& getOneObjectAtOffset( 0, -4, wall.class))
{
setLocation(getX()-4, getY());
}

}
Busch2207 Busch2207

2011/6/6

#
...
davmac davmac

2011/6/7

#
@Busch2207, that won't work either. If you create a new image, the old image will not compare equal to it. @MyHeartRocksNRolls: rather than using the image, just introduce a variable which controls the direction. You can set its value at the same time as you set the image. Put the variable inside your class:
public class Ghost
{
   private int direction;
.. and then check the direction:
public void act()
{
    if (direction == 0 && !getOneObjectAtOffset( 4, 0, wall.class)&& !getOneObjectAtOffset (-4, 0, wall.class)&& getOneObjectAtOffset( 0, 4, wall.class)&& getOneObjectAtOffset( 0, -4, wall.class))
    {
        setLocation(getX()+4, getY());
    }

    if (direction == 2 && !getOneObjectAtOffset( 4, 0, wall.class)&& !getOneObjectAtOffset (-4, 0, wall.class)&& getOneObjectAtOffset( 0, 4, wall.class)&& getOneObjectAtOffset( 0, -4, wall.class))
    {
        setLocation(getX()-4, getY());
    }
}
davmac davmac

2011/6/7

#
Minor additional change: getOneObjectAtOffset returns a reference, not a boolean, so change:
!getOneObjectAtOffset(...)
to:
getOneObjectAtOffset(...) == null
each time it occurs.
MyHeartRocksNRolls MyHeartRocksNRolls

2011/6/11

#
Thank you very much! It works without any problems and the using of a variable could be used in so many cases! This help make me move some steps forward.
You need to login to post a reply.