Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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  
  Enemy Speed  (Read 968 times)
0 Members and 1 Guest are viewing this topic.
Offline Graziano Mesina

Senior Newbie





« Posted 2006-04-27 10:43:04 »

Hi, I'm building a java game with tileMap

I've a Sprite class and some Enemy class that extends Sprite (ex. Enemy1, Enemy2, Enemy3....)

The Sprites are added in a LinkedList in the Tilemap Class with
1  
addSprite(map,enemy1Sprite,x,y);

 and are got with
1  
2  
 Iterator i = map.getSprites();
        while(i.hasNext())


Now I want to get velocity (speed for Enemy1 is differetn by Enemy2, Enemy3)

How I can Do this??

(some code example??)

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2006-04-27 13:20:07 »

You need to have an object that holds both speed (and position) and the sprite:


class Entity
{
    // position, speed, sprite
}

Iterator it = map.getEntities();
   while(it.hasNext())
      get pos/spd/sprite

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline Graziano Mesina

Senior Newbie





« Reply #2 - Posted 2006-04-27 16:23:55 »

My sprite class is like this:

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  
 
public class Sprite
{
    public Animation anim;
    float x;
    float y;
    float dx;
    float dy;   
   
    public Sprite(Animation anim)
    {
        this.anim = anim;       
        anim.start();
    }
   
    public void update(long elapsedTime)
    {       
        x += dx * elapsedTime
        anim.update(elapsedTime);
    }
   
    public void setX(float x)
    {
        this.x = x;
    }
   
    public void setY(float y)
    {
        this.y = y;
    }
   
    public float getX()
    {
        return x;
    }
   
    public float getY()
    {
        return y;
    }
   
    public void setDx(float dx)
    {
        this.dx = dx;
    }
   
    public void setDy(float dy)
    {
        this.dy = dy;
    }
   
    public float getDx()
    {
        return dx;
    }
   
    public float getDy()
    {
        return dy;
    }
   
    public Image getImage()
    {
        return anim.getImage();
    }
   
    public int getWidth()
    {
        return anim.getImage().getWidth(null);
    }
   
    public int getHeight()
    {
        return anim.getImage().getHeight(null);
    }
   
    public Object clone()
    {
        return new Sprite(anim);
    }     
}



and my Enemy1 class is:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public class Enemy1 extends Creature
{
    private Animation left;   
   
    public Enemy1(Animation left)
    {
        super(left);       
    }
}

So... I've to modify Sprite class and to insert Entity class??
    }

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #3 - Posted 2006-04-27 16:38:06 »

Use the
1  
code

tag. (the # button) Smiley
Offline Graziano Mesina

Senior Newbie





« Reply #4 - Posted 2006-04-29 15:04:22 »

help!!

-Montanelli-: Ma lei evadeva quasi sempre, no? <br /><br />-Mesina-: Sì, ho la fortuna di avere i polsi più grossi delle mani...
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2006-04-29 21:23:20 »

I see a dx and a dy.

Assuming thats dx per ms and dy per ms then distance per ms is Math.sqrt ((dx*dx)+(dy*dy))

If thats not your question then you are ging to have to explain it with a lot mroe detail, and I dontmean more code, I mean explain clearly what you are trying to accomplish.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
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.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!