Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Sprite animation dosen't work properly.  (Read 2601 times)
0 Members and 1 Guest are viewing this topic.
Offline SubzeroX5

Senior Newbie





« Posted 2011-11-05 22:50:23 »

I've made a sprite animation, and it won't animate to the next frame. Any ideas?

Here is the source code.
http://www.mediafire.com/?96bz06tdtyb15qo
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-11-06 00:12:54 »

Could you show us some code here on JGO? (nobody is gonna download a zip folder to be honest Smiley)

Offline SubzeroX5

Senior Newbie





« Reply #2 - Posted 2011-11-06 00:44:25 »

sure, here is the code.

Animation
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  
import java.awt.*;
import java.util.ArrayList;

public class Animation {

    public ArrayList animArray;
    public int f;
    public long animationTime;

    public Animation() {
        f = 0;
        animationTime = 0;
        animArray = new ArrayList();
    }

    public void addFrame(Image i, int t) {
        animArray.add(new Frame(i, t));
    }

    public void update(long time) {
        if (animArray.size() > 1) {
            if (animationTime < getFrame(f).time) {
                animationTime = time;
            } else if (animationTime > getFrame(f).time) {
                animationTime = 0;
                f++;
            }
        }
    }

    public Image getImage() {
        if (animArray.isEmpty()) {
            return null;
        } else {
            return getFrame(f).img;
        }
    }

    public Frame getFrame(int x) {
        return (Frame) animArray.get(x);
    }

    public class Frame {

        public Image img;
        public int time;

        public Frame(Image i, int t) {
            img = i;
            time = t;
        }
    }
}


AnimationTest
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
import javax.swing.JFrame;

public class AnimationTest {

    public AnimationTest() {
        JFrame f = new JFrame();
        f.setTitle("AnimationTest10");
        f.add(new MainConfig());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500, 500);
        f.setVisible(true);

    }

    public static void main(String[] args) {
        new AnimationTest();
    }
}


MainConfig
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  
import java.awt.Graphics;
import javax.swing.JPanel;

public class MainConfig extends JPanel implements Runnable {

    public SmileyFace smiley = new SmileyFace();
    public Thread mainLoop;

    public void MainConfig() {
        this.setDoubleBuffered(true);
        mainLoop = new Thread(this);
        mainLoop.start();
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(smiley.getFrame(), 250, 250, null);
    }

    @Override
    public void run() {
        long currentTime;
        long gameTime;
        long totalTime;
        while (true) {
            currentTime = System.currentTimeMillis();
            gameTime = System.currentTimeMillis() - currentTime;
            totalTime = +gameTime;
            smiley.allAnimations();
            smiley.currentAnimation.update(totalTime);
        }
    }
}


SmileyFace
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  
import java.awt.Image;
import javax.swing.ImageIcon;

public class SmileyFace{

    public Animation smileyAnim = new Animation();
    public Sprite currentAnimation;
   
    public SmileyFace(){
        currentAnimation = new Sprite(smileyAnimation());
    }
    private Animation smileyAnimation() {
        Image f1 = new ImageIcon(this.getClass().getResource("/animationtest/Face1.png")).getImage();
        Image f2 = new ImageIcon(this.getClass().getResource("/animationtest/Face2.png")).getImage();
        smileyAnim.addFrame(f1, 110);
        smileyAnim.addFrame(f2, 110);      
        return smileyAnim;
    }

    public void allAnimations(){
        smileyAnimation();  
    }
   
    public Sprite sprite() {
        return currentAnimation;
    }

    public Image getFrame() {
        return currentAnimation.getImage();
    }
}


Sprite
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  
import java.awt.Image;

public class Sprite {

    public int x, y;
    public Animation a;

    public Sprite(Animation anim) {
        a = anim;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return a.getImage().getWidth(null);
    }

    public int getHieght() {
        return a.getImage().getHeight(null);
    }
    public Image getImage(){
        return a.getImage();
    }
    public void update(long time){
        a.update(time);
    }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-11-06 01:11:07 »

Hehe you forgot to call repaint() in MainConfig after line 30 Grin

Offline Cero
« Reply #4 - Posted 2011-11-06 01:18:06 »

Hehe you forgot to call repaint() in MainConfig after line 30 Grin

yeah apart from that and calling
1  
2  
3  
4  
5  
6  
7  
Animation smileyAnimation() { 
        Image f1 = new ImageIcon(this.getClass().getResource("/animationtest/Face1.png")).getImage();
        Image f2 = new ImageIcon(this.getClass().getResource("/animationtest/Face2.png")).getImage();
        smileyAnim.addFrame(f1, 110);
        smileyAnim.addFrame(f2, 110);      
        return smileyAnim;
    }

every iteration, there is a lot else wrong... too much to list ^^

but I'm sure you naturally figure much of it out, as you work on it more

Offline SubzeroX5

Senior Newbie





« Reply #5 - Posted 2011-11-06 01:46:20 »

o woops, I did forget to put repaint() thanks.

But Cero, what do u mean by
"every iteration, there is a lot else wrong... too much to list ^^"

do you mean I have to many conditions that go to the else.

Or do you mean I have many things wrong in my code?
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-11-06 03:20:02 »

No he meant that you are calling smileyAnimation() every interation and that there are a lot of other problems with your code.
The problem with smileyAnimation() is that it is reading both images every time this method is called. This will cause your loop to slow down and it will hog a LOT of memory.

Offline ReBirth
« Reply #7 - Posted 2011-11-06 05:44:02 »

at least try to cache your image Smiley try to optimize bit is not ebil.

Offline SubzeroX5

Senior Newbie





« Reply #8 - Posted 2011-11-06 16:33:14 »

I've gotten some progress,and I've change the source code a little bit. But the new problem, is that if I were to change the value in Smiley animation to
1  
2  
3  
4  
5  
6  
    private void smileyAnimation() {
        Image f1 = new ImageIcon(this.getClass().getResource("/animationtest/Face1.png")).getImage();
        Image f2 = new ImageIcon(this.getClass().getResource("/animationtest/Face2.png")).getImage();
        smileyAnim.addFrame(f1, 10);
        smileyAnim.addFrame(f2, 10);      
    }


The animation starts on the second frame and stays their. But If I changed it to

1  
2  
3  
4  
5  
6  
    private void smileyAnimation() {
        Image f1 = new ImageIcon(this.getClass().getResource("/animationtest/Face1.png")).getImage();
        Image f2 = new ImageIcon(this.getClass().getResource("/animationtest/Face2.png")).getImage();
        smileyAnim.addFrame(f1, 30);
        smileyAnim.addFrame(f2, 30);      
    }


It stays on the first frame of the animation. Any thoughts?

Here are the other changes I've made in my source code.

Smiley Animation

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  
import java.awt.Image;
import javax.swing.ImageIcon;

public class SmileyFace{

    public Animation smileyAnim = new Animation();
    public Sprite currentAnimation;
   
    public SmileyFace(){
        smileyAnimation();
        currentAnimation = new Sprite(smileyAnim);
    }
    private void smileyAnimation() {
        Image f1 = new ImageIcon(this.getClass().getResource("/animationtest/Face1.png")).getImage();
        Image f2 = new ImageIcon(this.getClass().getResource("/animationtest/Face2.png")).getImage();
        smileyAnim.addFrame(f1, 30);
        smileyAnim.addFrame(f2, 30);      
    }

    public void allAnimations(){
        smileyAnimation();  
    }
   
    public Sprite sprite() {
        return currentAnimation;
    }

    public Image getFrame() {
        return currentAnimation.getImage();
    }
}


MainConfig

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  
import java.awt.Graphics;
import javax.swing.JPanel;

public class MainConfig extends JPanel implements Runnable {

    public SmileyFace smiley = new SmileyFace();
    public Thread mainLoop;

    public void MainConfig() {
        setDoubleBuffered(true);
        System.out.println("hi");
    }

    public void addNotify() {
        mainLoop = new Thread(this);
        mainLoop.start();
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        long totalTime = System.currentTimeMillis() - startTime;
        while (true) {
            long timePassed = System.currentTimeMillis() - totalTime;
            timePassed += totalTime;
            smiley.allAnimations();
            smiley.currentAnimation.update(timePassed);
            repaint();
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
        }
    }

    public void paint(Graphics g) {
        g.drawImage(smiley.getFrame(), 250, 250, null);
    }
}


Animation

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  
import java.awt.*;
import java.util.ArrayList;

public class Animation {

    public ArrayList animArray;
    public int f;
    public long animationTime;

    public Animation() {
        f = 0;
        animationTime = 0;
        animArray = new ArrayList();
    }

    public void addFrame(Image i, int t) {
        animArray.add(new Frame(i, t));
    }

    public void update(long time) {
        if (animArray.size() > 1) {
            animationTime = time;
            if (animationTime > getFrame(f).time) {
                f++;
                animationTime = 0;
            }else if(f > animArray.size()){
                f = 0;
            }
        }
        //System.out.println(time);
   }

    public Image getImage() {
        if (animArray.isEmpty()) {
            return null;
        } else {
            return getFrame(f).img;
        }
    }

    public Frame getFrame(int x) {
        return (Frame) animArray.get(x);
    }

    public class Frame {

        public Image img;
        public int time;

        public Frame(Image i, int t) {
            img = i;
            time = t;
        }
    }
}


Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-11-06 16:43:02 »

Don't forget to call "super.addNotify()" in MainConfig.
Also, you are supposed to add the images once, not every frame, so move the call to smileyAnimation() to the constructor.
Your animation class is completely wrong. You want each Frame to hold the time at which it will end, not the amount of time it is shown.
Take a look at my Animation class.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #10 - Posted 2011-11-06 17:00:09 »

What is also wierd is:

Sprite has an animation as a field
SmileyFace has a sprite and an animation

So SmileyFace has 2 animations D=

Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-11-06 17:17:16 »

Whoa......yeah you're right!
@OP
smiley.currentAnimation.update(timePassed) is calling the empty Animation inside Sprite, not the one that you have added the 2 images to Tongue

Offline SubzeroX5

Senior Newbie





« Reply #12 - Posted 2011-11-06 23:40:51 »

I'm making more progress now, but it still wouldn't animate. Any ideas?

Here is the new edited source codes.

Animation
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  
import java.awt.*;
import java.util.ArrayList;

public class Animation {

    public ArrayList<Frame> frames;
    public int f = 0;
    public long currentTime;
    public long totalTime;

    public Animation() {
        frames = new ArrayList();
        totalTime = 0;
        start();
    }

    public synchronized void addFrame(Image i, long t) {
        totalTime += t;
        frames.add(new Frame(i, totalTime));
    }

    public synchronized void start() {
        currentTime = 0;
        f = 0;
    }

    public synchronized void update(long time) {
        if (frames.size() > 1) {
            currentTime += time;
            if (currentTime >= totalTime) {
                currentTime = 0;
                f = 0;
            }
            while (currentTime > frames.get(f).time) {
                f++;
            }
        }
        //System.out.println(time);
   }

    public synchronized Image getImage() {
        if (frames.isEmpty()) {
            return null;
        } else {
            return frames.get(f).img;
        }
    }

    public class Frame {

        public Image img;
        public long time;

        public Frame(Image i, long t) {
            img = i;
            time = t;
        }
    }
}


MainConfig
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  
import java.awt.Graphics;
import javax.swing.JPanel;

public class MainConfig extends JPanel implements Runnable {

    public SmileyFace smiley = new SmileyFace();
    public Thread mainLoop;

    public void MainConfig() {
        setDoubleBuffered(true);
    }

    @Override
    public void addNotify() {
        super.addNotify();
        mainLoop = new Thread(this);
        mainLoop.start();
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        long totalTime = System.currentTimeMillis() - startTime;
        while (true) {
            long timePassed = System.currentTimeMillis() - totalTime;
            timePassed += totalTime;
            smiley.currentSprite.update(timePassed);
            repaint();
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(smiley.getFrame(), 250, 250, null);
        g.dispose();
    }
}


Offline SubzeroX5

Senior Newbie





« Reply #13 - Posted 2011-11-07 05:57:15 »

I just realized I've made a pretty careless mistake.

1  
2  
3  
    public void MainConfig() {
        setDoubleBuffered(true);
    }


should be

1  
2  
3  
    public MainConfig() {
        setDoubleBuffered(true);
    }


in MainConfig.
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2011-11-07 06:25:45 »

No need for that, JPanel is automatically double buffered Wink

Offline SubzeroX5

Senior Newbie





« Reply #15 - Posted 2011-11-08 05:30:15 »

Thanks for your reply.

Here is the new MainConfig
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  
import java.awt.Graphics;
import javax.swing.JPanel;

public class MainConfig extends JPanel implements Runnable {

    public SmileyFace smiley;
    public Thread mainLoop;

    public MainConfig() {
        smiley = new SmileyFace();
    }

    @Override
    public void addNotify() {
        super.addNotify();
        mainLoop = new Thread(this);
        mainLoop.start();
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        long totalTime = System.currentTimeMillis() - startTime;
        while (true) {
            long timePassed = System.currentTimeMillis() - totalTime;
            timePassed += totalTime;
            smiley.sprite().update(timePassed);
            repaint();
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
        }

    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(smiley.getFrame(), 250, 250, null);
    }
}


If you don't mind, could you tell me why my animation isn't working or why it isn't animating.
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2011-11-08 06:09:57 »

Your methods seem to be different from the last SmileyFace class you posted. Could you post it again?

Offline SubzeroX5

Senior Newbie





« Reply #17 - Posted 2011-11-08 16:18:24 »


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  
import java.awt.Image;
import javax.swing.ImageIcon;

public class SmileyFace{

    public Animation smileyAnim;
    public Sprite currentSprite;

    public SmileyFace() {
        smileyAnimation();
    }

    public void smileyAnimation() {
        smileyAnim = new Animation();
        Image f1 = new ImageIcon(this.getClass().getResource("/animationtest/Face1.png")).getImage();
        Image f2 = new ImageIcon(this.getClass().getResource("/animationtest/Face2.png")).getImage();
        smileyAnim.addFrame(f1, 30);
        smileyAnim.addFrame(f2, 30);    
        currentSprite = new Sprite(smileyAnim);
    }

    public Sprite sprite() {
        return currentSprite;
    }

    public Image getFrame() {
        return currentSprite.getImage();
    }
}
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2011-11-08 19:29:10 »

Ok so you call smiley.sprite().update(timePassed) each frame. That update method calls the Animation inside Sprite. So either there is something wrong with your Animation class, or the loop isn't running, or the screen won't update. Your Animation class seems fine, except for this line in your update method: "currentTime = 0", that should be "currentTime %= totalTime;"
Try making sure your loop is actually running by putting a println in there. Then try making sure paint() is actually being called by also putting a println. If everything is working, try using a debugger to see why it won't update because everything seems fine to me.


I found the problem: you not calculating timePassed correctly in your game loop. totalTime will always equal 0 so timePassed will always be System.currentTimeMillis(). The correct way of calculating timePassed:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
long lastTime = System.currentTimeMillis();
while(true) {
    long now = System.currentTimeMillis();
    long timePassed = now-lastTime;
    lastTime = now;
   
    //update
   
    repaint():
   
    //Here I accounted for the amount of time it took to update and render so you can have a reliable and steady loop.
   try{
        Thread.sleep(10-(System.currentTimeMillis()-now));
    }
    catch(Exception exc) {
    }
}

Offline SubzeroX5

Senior Newbie





« Reply #19 - Posted 2011-11-08 20:00:58 »

Thank You! It works perfectly now!  Grin
Offline SubzeroX5

Senior Newbie





« Reply #20 - Posted 2011-11-08 22:11:40 »

But I have a new problem. I'm trying to make a new game, and when I press left, my character won't face left. He just faces right(His default is facing right).

Is their a way to fix it so that when I press the right key, he faces right and when I press the left key, he faces left?

Here are the source codes

Animation
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  
import java.awt.*;
import java.util.ArrayList;

public class Animation {

    public ArrayList<Frame> frames;
    public int f = 0;
    public long currentTime;
    public long totalTime;

    public Animation() {
        frames = new ArrayList();
        totalTime = 0;
        start();
    }

    public synchronized void addFrame(Image i, long t) {
        totalTime += t;
        frames.add(new Frame(i, totalTime));
    }

    public synchronized void start() {
        currentTime = 0;
        f = 0;
    }

    public synchronized void update(long time) {
        if (frames.size() > 1) {
            currentTime += time;
            if (currentTime >= totalTime) {
                currentTime = 0;
                f = 0;
            }
            while (currentTime > frames.get(f).time) {
                f++;
                System.out.println("next frame");
            }
        }
        //System.out.println(time);
   }

    public synchronized Image getImage() {
        if (frames.isEmpty()) {
            return null;
        } else {
            return frames.get(f).img;
        }
    }

    public class Frame {

        public Image img;
        public long time;

        public Frame(Image i, long t) {
            img = i;
            time = t;
        }
    }
}


ExtremeMario
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
import javax.swing.JFrame;

public class ExtremeMario {

    public ExtremeMario() {
        JFrame f = new JFrame();
        f.setTitle("ExtremeMario");
        f.add(new MainConfig());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500, 500);
        f.setVisible(true);

    }

    public static void main(String[] args) {
        new ExtremeMario();
    }
}


MainConfig
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  
import extrememario.mario.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class MainConfig extends JPanel implements Runnable {

    public Mario mario;
    public Thread mainLoop;

    public MainConfig() {
        mario = new Mario();
        this.addKeyListener(new KL());

    }

    @Override
    public void addNotify() {
        super.addNotify();
        mainLoop = new Thread(this);
        mainLoop.start();
    }

    @Override
    public void run() {
        long lastTime = System.currentTimeMillis();
        while (true) {
            long nowTime = System.currentTimeMillis();
            long timePassed = nowTime - lastTime;
            lastTime = nowTime;
            mario.sprite().update(timePassed);
            repaint();
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
        }

    }
    public Image bg = new ImageIcon(this.getClass().getResource("/extrememario/BackgroundDemoStage.png")).getImage();

    @Override
    public void paint(Graphics g) {
        g.drawImage(bg, 0, 0, null);
        g.drawImage(mario.getFrame(), 250, 250, null);
        g.dispose();
    }

    public class KL implements KeyListener {

        @Override
        public void keyPressed(KeyEvent e) {
            mario.keyPressed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            mario.keyReleased(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {
        }
    }
}


Sprite
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  
import java.awt.Image;

public class Sprite {

    public int x, y;
    public Animation a;

    public Sprite(Animation anim) {
        a = anim;
    }

    public void update(long time) {
        a.update(time);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return a.getImage().getWidth(null);
    }

    public int getHieght() {
        return a.getImage().getHeight(null);
    }

    public Image getImage() {
        return a.getImage();
    }
}


Mario
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  
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;

/**
 *
 * @author ben
 */

public class Mario extends MarioSprites implements KeyListener {

    public Mario() {
        idleRight();
    }
    public boolean ap;//stands for already pressed

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
            idleRight();

        } else if (keyCode == KeyEvent.VK_LEFT) {
            idleLeft();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
            idleRight();

        } else if (keyCode == KeyEvent.VK_LEFT) {
            idleLeft();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
}


MarioSprites
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  
import extrememario.Animation;
import extrememario.Sprite;
import java.awt.Image;
import javax.swing.ImageIcon;

public class MarioSprites {

    public Animation idleAnimRight;
    public Sprite currentSprite;

    public void idleRight() {
        idleAnimRight = new Animation();
        Image f1 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame0.png")).getImage();
        Image f2 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame1.png")).getImage();
        Image f3 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame2.png")).getImage();
        Image f4 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame3.png")).getImage();
        Image f5 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame4.png")).getImage();
        idleAnimRight.addFrame(f1, 110);
        idleAnimRight.addFrame(f2, 110);
        idleAnimRight.addFrame(f3, 110);
        idleAnimRight.addFrame(f4, 110);
        idleAnimRight.addFrame(f5, 110);
        currentSprite = new Sprite(idleAnimRight);
    }
    public Animation idleAnimLeft;

    public void idleLeft() {
        idleAnimRight = new Animation();
        Image f1 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft1.png")).getImage();
        Image f2 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft2.png")).getImage();
        Image f3 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft3.png")).getImage();
        Image f4 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft4.png")).getImage();
        Image f5 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft5.png")).getImage();
        idleAnimLeft.addFrame(f1, 110);
        idleAnimLeft.addFrame(f2, 110);
        idleAnimLeft.addFrame(f3, 110);
        idleAnimLeft.addFrame(f4, 110);
        idleAnimLeft.addFrame(f5, 110);
        currentSprite = new Sprite(idleAnimLeft);
    }

    public Sprite sprite() {
        return currentSprite;
    }

    public Image getFrame() {
        return currentSprite.getImage();
    }
}
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #21 - Posted 2011-11-08 23:17:03 »

Check to see if the idleLeft() method is actually called. Also it is not good to reload all the images in the idleLeft() and idleRight(). You should only load your resources in memory once or else it will slow down your entire game.

Offline SubzeroX5

Senior Newbie





« Reply #22 - Posted 2011-11-09 04:48:59 »

Thanks for the reply.

Yea, I checked, idleLeft() is called.

It starts from MarioSprites
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
    public Animation idleAnimLeft;
    public Image l1 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft1.png")).getImage();
    public Image l2 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft2.png")).getImage();
    public Image l3 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft3.png")).getImage();
    public Image l4 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft4.png")).getImage();
    public Image l5 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft5.png")).getImage();

    public void idleLeft() {
        idleAnimLeft = new Animation();
        idleAnimLeft.addFrame(l1, 110);
        idleAnimLeft.addFrame(l2, 110);
        idleAnimLeft.addFrame(l3, 110);
        idleAnimLeft.addFrame(l4, 110);
        idleAnimLeft.addFrame(l5, 110);
    }


to Mario
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
@Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
            idleRight();
            currentSprite = new Sprite(idleAnimRight);

        } else if (keyCode == KeyEvent.VK_LEFT) {
            idleLeft();
            currentSprite = new Sprite(idleAnimLeft);
        }
    }


to
MainConfig

1  
2  
3  
4  
5  
    public MainConfig() {
        mario = new Mario();
        addKeyListener(new KL());

    }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
    public class KL implements KeyListener {

        @Override
        public void keyPressed(KeyEvent e) {
            mario.keyPressed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            mario.keyReleased(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {
        }
    }


Here are all the changed source codes(whole classes)

MainConfig
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  
import extrememario.mario.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class MainConfig extends JPanel implements Runnable {

    public Mario mario;
    public Thread mainLoop;

    public MainConfig() {
        mario = new Mario();
        addKeyListener(new KL());

    }

    @Override
    public void addNotify() {
        super.addNotify();
        mainLoop = new Thread(this);
        mainLoop.start();
    }

    @Override
    public void run() {
        long lastTime = System.currentTimeMillis();
        while (true) {
            long nowTime = System.currentTimeMillis();
            long timePassed = nowTime - lastTime;
            lastTime = nowTime;
            mario.sprite().update(timePassed);
            repaint();
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
        }

    }
    public Image bg = new ImageIcon(this.getClass().getResource("/extrememario/BackgroundDemoStage.png")).getImage();

    @Override
    public void paint(Graphics g) {
        g.drawImage(bg, 0, 0, null);
        g.drawImage(mario.getFrame(), 250, 250, null);
        g.dispose();
    }

    public class KL implements KeyListener {

        @Override
        public void keyPressed(KeyEvent e) {
            mario.keyPressed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            mario.keyReleased(e);
        }

        @Override
        public void keyTyped(KeyEvent e) {
        }
    }
}


Mario
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  
import extrememario.Sprite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;

/**
 *
 * @author ben
 */

public class Mario extends MarioSprites implements KeyListener {

    public Mario() {
        idleRight();
        currentSprite = new Sprite(idleAnimRight);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
            idleRight();
            currentSprite = new Sprite(idleAnimRight);

        } else if (keyCode == KeyEvent.VK_LEFT) {
            idleLeft();
            currentSprite = new Sprite(idleAnimLeft);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
        } else if (keyCode == KeyEvent.VK_LEFT) {
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
}


MarioSprites
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  
import extrememario.Animation;
import extrememario.Sprite;
import java.awt.Image;
import javax.swing.ImageIcon;

public class MarioSprites {

    public Animation idleAnimRight;
    public Sprite currentSprite;
   
    public Image r1 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame0.png")).getImage();
    public Image r2 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame1.png")).getImage();
    public Image r3 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame2.png")).getImage();
    public Image r4 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame3.png")).getImage();
    public Image r5 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame4.png")).getImage();

    public void idleRight() {
        idleAnimRight = new Animation();
        idleAnimRight.addFrame(r1, 110);
        idleAnimRight.addFrame(r2, 110);
        idleAnimRight.addFrame(r3, 110);
        idleAnimRight.addFrame(r4, 110);
        idleAnimRight.addFrame(r5, 110);
    }
    public Animation idleAnimLeft;
    public Image l1 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft1.png")).getImage();
    public Image l2 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft2.png")).getImage();
    public Image l3 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft3.png")).getImage();
    public Image l4 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft4.png")).getImage();
    public Image l5 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft5.png")).getImage();

    public void idleLeft() {
        idleAnimLeft = new Animation();
        idleAnimLeft.addFrame(l1, 110);
        idleAnimLeft.addFrame(l2, 110);
        idleAnimLeft.addFrame(l3, 110);
        idleAnimLeft.addFrame(l4, 110);
        idleAnimLeft.addFrame(l5, 110);
    }

    public Sprite sprite() {
        return currentSprite;
    }

    public Image getFrame() {
        return currentSprite.getImage();
    }
}
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #23 - Posted 2011-11-09 05:02:43 »

I took a good look at everything and all seems fine to me :S
Try using a debugger Smiley

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #24 - Posted 2011-11-09 07:43:51 »

see what happens when you set the default to left? cuase then that'll telly ou whether your whole method of doing it is wrong, or just the implementation
Offline SubzeroX5

Senior Newbie





« Reply #25 - Posted 2011-11-11 19:58:44 »

When I make,

1  
2  
3  
4  
5  
6  
    public Mario() {
        idleLeft();
        currentSprite = new Sprite(idleAnimLeft);
        currentSprite.x = 250;
        currentSprite.y = 250;
    }


he faces left, but when I press right, he won't face right.
So far, I've figured out that the keyPressed in Mario isn't actually getting called, but I can't seem to figure out why. Any idea's or thoughts to fix this?

Here are all the changed soruce codes

Mario
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  
public class Mario extends MarioSprites{

    public Mario() {
        idleLeft();
        currentSprite = new Sprite(idleAnimLeft);
        currentSprite.x = 250;
        currentSprite.y = 250;
    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
            System.out.println("Right key");
            idleRight();
            currentSprite = new Sprite(idleAnimRight);        

        } else if (keyCode == KeyEvent.VK_LEFT) {
            idleLeft();
            currentSprite = new Sprite(idleAnimLeft);
        }
    }

    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_RIGHT) {
        } else if (keyCode == KeyEvent.VK_LEFT) {
        }
    }

    public void keyTyped(KeyEvent e) {
    }
}


MainConfig
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  
import extrememario.mario.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class MainConfig extends JPanel implements Runnable {

    public Mario mario;
    public Thread mainLoop;

    public MainConfig() {
        mario = new Mario();
        addKeyListener(new KL());
        System.out.println();
    }

    @Override
    public void addNotify() {
        super.addNotify();
        mainLoop = new Thread(this);
        mainLoop.start();
    }

    public Image bg = new ImageIcon(this.getClass().getResource("/extrememario/BackgroundDemoStage.png")).getImage();

    @Override
    public void paint(Graphics g) {
        g.drawImage(bg, 0, 0, null);
        g.drawImage(mario.getFrame(), mario.currentSprite.getX(), mario.currentSprite.getY(), null);
        g.dispose();
    }

    public class KL extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            mario.keyPressed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            mario.keyReleased(e);
        }
    }
        @Override
    public void run() {
        long lastTime = System.currentTimeMillis();
        while (true) {
            long nowTime = System.currentTimeMillis();
            long timePassed = nowTime - lastTime;
            lastTime = nowTime;
            mario.sprite().update(timePassed);            
            repaint();
            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
        }

    }
}


MarioSprites
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  
import extrememario.Animation;
import extrememario.Sprite;
import java.awt.Image;
import javax.swing.ImageIcon;

public class MarioSprites {

    public Animation idleAnimRight;
    public Sprite currentSprite;
    public int sx,sy;
   
    public Image r1 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame0.png")).getImage();
    public Image r2 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame1.png")).getImage();
    public Image r3 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame2.png")).getImage();
    public Image r4 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame3.png")).getImage();
    public Image r5 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/IRFrame4.png")).getImage();

    public void idleRight() {
        idleAnimRight = new Animation();
        idleAnimRight.addFrame(r1, 110);
        idleAnimRight.addFrame(r2, 110);
        idleAnimRight.addFrame(r3, 110);
        idleAnimRight.addFrame(r4, 110);
        idleAnimRight.addFrame(r5, 110);
    }
    public Animation idleAnimLeft;
    public Image l1 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft1.png")).getImage();
    public Image l2 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft2.png")).getImage();
    public Image l3 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft3.png")).getImage();
    public Image l4 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft4.png")).getImage();
    public Image l5 = new ImageIcon(this.getClass().getResource("/extrememario/mario/sprites/ILeft5.png")).getImage();

    public void idleLeft() {
        idleAnimLeft = new Animation();
        idleAnimLeft.addFrame(l1, 110);
        idleAnimLeft.addFrame(l2, 110);
        idleAnimLeft.addFrame(l3, 110);
        idleAnimLeft.addFrame(l4, 110);
        idleAnimLeft.addFrame(l5, 110);
    }

    public Sprite sprite() {
        return currentSprite;
    }

    public Image getFrame() {
        return currentSprite.getImage();
    }
}
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #26 - Posted 2011-11-11 22:20:50 »

Check to see if the methods under KL are being called when you press the keys.

Offline philfrei
« Reply #27 - Posted 2011-11-11 22:43:07 »

Handling KeyStrokes has always confused me.  Clueless

It looks like you are adding the keyListener to "this", i.e., MainConfig, but I don't see where MainConfig implements a KeyListener. Does a JPanel implement KeyListener by default or does it have to be specified?

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 338
Projects: 2
Exp: 5 years


I'm the King!


« Reply #28 - Posted 2011-11-11 22:51:34 »

@philfrei
The line "addKeyListener(new KL());" is what adds the listener to the JPanel.

Offline ReBirth
« Reply #29 - Posted 2011-11-12 05:43:30 »

or "this.addKeyListener(new KeyListenerClass());"

it's not confusing, share the listened event among all class (like lwjgl) is.

Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (15 views)
2014-07-29 18:09:19

Riven (10 views)
2014-07-29 18:08:52

Dwinin (10 views)
2014-07-29 10:59:34

E.R. Fleming (28 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!