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

2021/4/15

How to animate multiple sprites?

1
2
Samuaelk Samuaelk

2021/4/21

#
I put the line into the code, but instead of the images loading, it is only showing a green foot logo Here is the revised Hero class code.
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public class Hero extends Mover
{
    String imgSet;
    GreenfootImage[] images = new GreenfootImage[8];
 
    private static final int jumpStrength = 20;
    private int level;
    private int jumped = 0;
 
     
    public Hero()
    {
 
        changeImage(1);
         
        level = 1;
    }
 
    public void changeImage (int x)
    {
        if (x == 1)
        {
            imgSet = "wizard_1";
        }
        else if (x == 2)
        {
            imgSet = "princess";
        }
        else if (x == 3)
        {
            imgSet = "goblin_2";
        }
        else if (x == 4)
        {
            imgSet = "barbarian_1";
        }
        for (int i=0; i<8; i++)
            images[i] = new GreenfootImage(imgSet+"_run_00"+(i+1)+".png");
    }
 
    public void act()
    {
        if (getWorld() instanceof CharSelect) return;
        checkKeys();       
        checkFall();
        checkNextLevel();
        landOnTop();
 
    }
 
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
 
            moveLeft();
        }
        if (Greenfoot.isKeyDown("right") )
        {
 
            moveRight();
        }
        if (Greenfoot.isKeyDown("up") )
        {
            if (onGround())
                jump();
        }
    }   
 
    private void jump()
    {
        vSpeed= -jumpStrength;
        fall();
        jumped++;
    }
 
    public void landOnTop()
    {
        if (isTouching(Ground.class))
        {
            setLocation(getX(), getY() - 1);
        }
    }
 
    private void checkFall()
    {
        if (onGround()) {
            setVSpeed(0);
        }
        else {
            fall();
        }
    }
 
    /**
     * Check whether we should go to the next level, and if yes, start the next level.
     */
    private void checkNextLevel()
    {
        if (getX() == getWorld().getWidth()-1) {
            if (level == 1) {
                level = 2;
                getWorld().removeObject(this);
                Greenfoot.setWorld(new Level2(this));
            }
            else {
                level = 1;
                getWorld().removeObject(this);
                Greenfoot.setWorld(new Level1(this));
            }
        }
    }
 
}
danpost danpost

2021/4/21

#
Samuaelk wrote...
I put the line into the code, but instead of the images loading, it is only showing a green foot logo
danpost wrote...
At the end of the method, use a for loop to load the images into the array and, after that, set the first image to the hero.
Samuaelk Samuaelk

2021/4/21

#
To set the image do I do what I previously had in my code? Like this part?
1
2
3
4
if (x == 1)
        {
            setImage(i1);
        }
Samuaelk Samuaelk

2021/4/22

#
Please help me with this, ASAP, because this is for an important assignment. Sorry for the trouble
danpost danpost

2021/4/25

#
Samuaelk wrote...
To set the image do I do what I previously had in my code? Like this part? << Code Omitted >>
No. Use:
1
setImage(images[0]);
You need to login to post a reply.
1
2