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]
  ignore  |  Print  
  animation  (Read 1673 times)
0 Members and 1 Guest are viewing this topic.
Offline neoskunk

Junior Member





« Posted 2008-10-03 22:37:13 »

ok here is my problem. i have the following code that draws a character and a level.  pressing right or left on the arrow keys will move the character forward or backward.  up and down raise or lower the angle to shoot at.  holding space will increase power and when space is released the character shoots a bullet.  however when the bullet hits the ground i want it to disappear and i want to animate an explosion.    so basically what is a good way to do multiple animations?  in particular whats a good way to make animations appear and disappear?

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  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class Test extends Applet implements Runnable, KeyListener {
    int x = 0;
    int y = 500;
    int z = 0;
    int q = 44;
    int d=0, p =0;
   
    double xfloat = 0;
    double yfloat = 0;
    double vx = 0,vy = 0;
    double angle = 0;
    double power = 0;
    final double gravity = 0.3;
    double pi = 2*Math.PI;
   
    Thread th = new Thread(this);
    BufferedImage offscreen = new BufferedImage(1000,600,BufferedImage.TYPE_INT_RGB);
    Graphics2D bufferGraphics = offscreen.createGraphics();
    boolean keys[];
    boolean paused;
    Image sprite[] = new Image[30];

   
    public void init() {
        setBackground(Color.white);
        addKeyListener(this);
        keys = new boolean[65536];
        th.start();
        Characters create = new Characters();
        for (int i = 0; i < 30; i++)
        {
            sprite[i] = create.createBSprite(q);
            q--;
        }
       
        paused = false;
    }

    public void start() {
        paused = false;
    }
   
    public void stop() {
        paused = true;
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
        keys[e.getKeyCode()] = false;
    }

    public void keyPressed(KeyEvent e) {
        keys[e.getKeyCode()] = true;
    }

    //override paint and update to stop any system rendering
 
    public void paint(Graphics g) {}
    public void update(Graphics g) {}

    public void run() {
        while (true) {
            //game loop
           if(!paused) {
                Graphics2D g = (Graphics2D)getGraphics();
                bufferGraphics.setColor(Color.WHITE);
                bufferGraphics.fillRect(0, 0, getWidth(), getHeight());
                //game logic
               if(xfloat != 0 && yfloat != 0)
                {
                        xfloat += vx;
                        yfloat += vy;
                        vy += 0.3;
                }
               
               
                if(keys[KeyEvent.VK_RIGHT] && x < 374) {
                            x++;
                }
                if(keys[KeyEvent.VK_LEFT] && x > -3) {
                    x--;
                }
               
                if(keys[KeyEvent.VK_UP] && z < 15)  {
                    z++;
                    angle += 5;
                }
               
                if(keys[KeyEvent.VK_DOWN] && z > 0) {
                    z--;
                    angle -= 5;
                }
               
               
                if(keys[KeyEvent.VK_SPACE] ) {
                    d = 10;
                    p = 10;
                    xfloat = x+63;
                    yfloat = y+20;
                    power += 2;
                    vx = (power / 10.0) * (float)Math.cos((pi * (double)angle) / 360.0);
                    vy = -((power / 10.0) * (float)Math.sin((pi * (double)angle) / 360.0));
                   
                }
               
               
                if (xfloat > 450 && xfloat < 550 && yfloat > 280 && yfloat < 290)
                    vy = -((power / 15.0) * (float)Math.sin((pi * (double)angle) / 360.0));
               
                if(yfloat > 564 || xfloat > 1000)
                {
                    power = 0;
                    d = 0;
                    p = 0;
                    xfloat = 0;
                    yfloat = 0;
                   
                }
               
                   
                   
               
               
                //rendering
             
                bufferGraphics.setColor(Color.red);
                bufferGraphics.fillRect(0,300,30,300);
                bufferGraphics.fillRect(450,300,100,300);
                bufferGraphics.setColor(Color.green);
                bufferGraphics.fillRect(0,565,1000,35);
                bufferGraphics.setColor(Color.magenta);
                bufferGraphics.fillOval((int)xfloat,(int)yfloat,d,p);
                bufferGraphics.setColor(Color.blue);
                bufferGraphics.fillRect(0,0,(int)power,10);
                bufferGraphics.drawImage(sprite[z], x, y,this);
                g.drawImage(offscreen,0,0,this);
                g.dispose();
               
               
               

               
                try {
                    Thread.sleep(20);
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            else {
                try {
                    Thread.yield();
                    Thread.sleep(100);
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
       
    }
   
   

}

class Characters
{
 BufferedImage createBSprite(int x)
        {
            BufferedImage sprite = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2D = sprite.createGraphics();
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
            g2D.fillRect(0,0,100,100);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
            g2D.setColor(Color.black);
            //head
           g2D.fillOval(42,20,17,16);
            //neck
           g2D.fillRect(48,35,7,4);
            //body
           g2D.fillArc(42,36,17,16,0,180);
            g2D.fillRect(43,44,16,10);
            //left leg and foot
           g2D.fillRect(44,54,3,8);
            g2D.fillRect(44,62,6,3);
            //right leg and foot
           g2D.fillRect(55,54,3,8);
            g2D.fillRect(55,62,6,3);
            //right arm
           //g2D.fillRect(58,44,10,3);
           //g2D.fillOval(64,41,12,8);
           g2D.drawLine(58,44,68,x);
            g2D.drawLine(58,45,68,x+1);
            g2D.drawLine(58,46,68,x+2);
            //left arm
           g2D.fillRect(36,44,8,3);
            g2D.fillOval(32,43,5,5);
            return sprite;
       
        }
       
       
    }
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2008-10-04 07:52:29 »

Typically you want to do something like having a list of objects/animations in your game, and then give them their own run() method. The main game loop calls this run method, which then through the use of local variables (each Animation is its own instantiation), the animation updates itself. Then when it's finished it can tell the main game to delete it by removing it from the list.

I will usually make Explosion and Bullet different objects, and when a Bullet impacts with something it instantiates an Explosion. The Explosion applies force/damage to the stuff around it, meanwhile playing its animation. And as I said before, it deletes itself when it's done.

Really what you want to think about is the encapsulation of your animations and objects, and give them their own respective timers. The main class should not handle that sort of thing.

See my work:
OTC Software
Offline neoskunk

Junior Member





« Reply #2 - Posted 2008-10-04 22:55:09 »

first of all sorry for posting this twice. i just figured it would go better in this section of the forum.  anyways that makes more sense than lots of threads but i dont quite understand how to do this.   could you post a short example program?  or do you know of any examples of this on the internet? 
Pages: [1]
  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 (12 views)
2014-07-29 18:09:19

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

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

E.R. Fleming (26 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 (28 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!