following a moving sprite
Posted
by
iQue
on Game Development
See other posts from Game Development
or by iQue
Published on 2012-08-28T19:40:58Z
Indexed on
2012/08/28
21:52 UTC
Read the original article
Hit count: 295
Im trying to get my enemies to follow my main-character of the game (2D), but for some reason the game starts lagging like crazy when I do it the way I want to do it, and the following-part dosnt work 100% either, its just 1/24 enemies that comes to my sprite, the other 23 move towards it but stay at a certain point. Might be a poor explenation but dont know how else to put it.
Code for moving my enemies:
private int enemyX(){
int x = 0;
for (int i = 0; i < enemies.size(); i++){
if (controls.pointerPosition.x > enemies.get(i).getX()){//pointerPosition is the position of my main-sprite.
x = 5;
}
else{
x=-5;
}
Log.d(TAG, "happyX HERE: " + controls.pointerPosition.x);
Log.d(TAG, "enemyX HERE: " + enemies.get(i).getX());
}
return x;
}
private int enemyY(){
int y = 0;
for (int i = 0; i < enemies.size(); i++){
if (controls.pointerPosition.y > enemies.get(i).getY()){
y = 5;
}
else{
y=-5;
}
}
return y;
}
I send it to the update-method in my Enemy-class:
private void drawEnemy(Canvas canvas){
addEnemies(); // a method where I add enemies to my arrayList, no parameters except bitmap.
for(int i = 0; i < enemies.size(); i++){
enemies.get(i).update(enemyX(), enemyY());
}
for(int i = 0; i < enemies.size(); i++){
enemies.get(i).draw(canvas);
}
}
and finally, the update-method itself, located in my Enemy-class:
public void update(int velX, int velY) {
x += velX; //sets x before I draw
y += velY; //sets y before I draw
currentFrame = ++currentFrame % BMP_COLUMNS;
}
So can any1 figure out why it starts lagging so much and how I can fix it? Thanks for your time!
© Game Development or respective owner