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

2015/5/5

Is it possible to check if an actor is not moving?

Srax Srax

2015/5/5

#
Hey, i am making a game, where i need to know if the actor isen't moving. If i have been moving left, and is no longer moving, it should play the gif: "IdleLeftGif.gif". If i have been moving right, and is no longer moving, it should play the gif: "IdleRightGif.gif". I already know how to tell what side it is looking, but i need to know if there is a way to tell if the actor is moving or not. Thank you in advance :) Here is the code for my actor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Giftest here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Giftest extends Actor
{
    /**
     * Act - do whatever the Giftest wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    GifImage gifImage = new GifImage("idleLeftGif.gif");
    GifImage2 gifImage2 = new GifImage2("moveLeftGif.gif");
    GifImage3 gifImage3 = new GifImage3("moveRightGif.gif");
    GifImage4 gifImage4 = new GifImage4("idleRightGif.gif");
    GifImage5 gifImage5 = new GifImage5("attackLeftGif.gif");
    GifImage6 gifImage6 = new GifImage6("attackRightGif.gif");
    int isLooking = 0;
    public void act()
    {
        getImage().scale(70, 70);
        moveLeft();
        moveRight();
        kill();
        attackLeft();
    }
    public void moveLeft()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            move(-2);
            setImage(gifImage2.getCurrentImage());
            getImage().scale(70, 70);
            isLooking = 0;
        }
    }
    public void moveRight()
    {
        {
            if (Greenfoot.isKeyDown("right"))
            {
                move(2);
                setImage(gifImage3.getCurrentImage());
                getImage().scale(70, 70);
                isLooking = 1;
            }
        }
    }
    public void attackLeft()
    {
        if (isLooking == 0)
        {
            if (Greenfoot.isKeyDown("space"))
            {
                setImage(gifImage6.getCurrentImage());
            }           
        }
    }
    public void attackRight()
    {
        if (isLooking == 1)
        {
            if (Greenfoot.isKeyDown("space"))
            {
                setImage(gifImage5.getCurrentImage());
            }           
        }
    }
    public void kill()
    {
        if (this.isTouching(Enemy.class)) //Hvis bullet kommer i kontakt med Enemy
        {
           Actor enemy;
           enemy = getOneObjectAtOffset(0, 0, Enemy.class); //Tager Enemys koordinater
           if (enemy != null)
           {
               World world;
               world = getWorld();
               world.removeObject(enemy); //Fjerner Enemy
            }
        }
    }
}
danpost danpost

2015/5/5

#
I would probably have the 'moveLeft' and 'moveRight' methods return boolean values to indicate whether the actor moved or not. If the returned boolean values are the same (both true or both false), then no movement has occurred (in total). That, with the 'isLooking' field, can determine which gif to use.
Srax Srax

2015/5/8

#
danpost wrote...
I would probably have the 'moveLeft' and 'moveRight' methods return boolean values to indicate whether the actor moved or not. If the returned boolean values are the same (both true or both false), then no movement has occurred (in total). That, with the 'isLooking' field, can determine which gif to use.
I am fairly new at programming, and to be honest... i kinda suck at it. Can you please show me how to do it? :)
danpost danpost

2015/5/8

#
The following is the 'moveRight' method that returns a boolean value indicating whether the move to the right was made or not. You would change the 'moveLeft' method similarly.
1
2
3
4
5
6
7
8
9
10
11
12
public boolean moveRight()
{
    if (Greenfoot.isKeyDown("right"))
    {
        move(2);
        setImage(gifImage3.getCurrentImage());
        getImage().scale(70, 70);
        isLooking = 1;
        return true;
    }
    return false;
}
Srax Srax

2015/5/8

#
danpost wrote...
The following is the 'moveRight' method that returns a boolean value indicating whether the move to the right was made or not. You would change the 'moveLeft' method similarly.
1
2
3
4
5
6
7
8
9
10
11
12
public boolean moveRight()
{
    if (Greenfoot.isKeyDown("right"))
    {
        move(2);
        setImage(gifImage3.getCurrentImage());
        getImage().scale(70, 70);
        isLooking = 1;
        return true;
    }
    return false;
}
Thank you :)
You need to login to post a reply.