Search code examples
javaslick2d

Speed Increases with Every New instance of Object


I'm having an issue in my game where the more objects that spawn, the faster these instances of the objects go. They almost double their speed every time a new one spawns.

This is what is currently used to move the objects:

public void goRight(){
    this.setX((this.getX()+this.getSpeed()));
    this.isMoving=true;
    this.stopMovingLeft();
    this.stopMovingUp();
    this.stopMovingDown();
    this.stopMovingUpRight();
    this.stopMovingUpLeft();
    this.stopMovingDownLeft();
    this.stopMovingDownRight();
    this.isMovingRight=true;
}

public void goLeft(){
    this.setX((this.getX()-this.getSpeed()));
    this.isMoving=true; 
    this.stopMovingRight(); 
    this.stopMovingUp(); 
    this.stopMovingDown(); 
    this.stopMovingUpRight(); 
    this.stopMovingUpLeft(); 
    this.stopMovingDownLeft(); 
    this.stopMovingDownRight(); 
    this.isMovingLeft=true;
}

public void goUp(){
    this.setY((this.getY()-this.getSpeed())); 
    this.isMoving=true; 
    this.stopMovingLeft(); 
    this.stopMovingRight(); 
    this.stopMovingDown(); 
    this.stopMovingUpRight(); 
    this.stopMovingUpLeft(); 
    this.stopMovingDownLeft(); 
    this.stopMovingDownRight(); 
    this.isMovingUp=true;
}

public void goDown(){
    this.setY((this.getY()+this.getSpeed())); 
    this.isMoving=true; 
    this.stopMovingLeft(); 
    this.stopMovingRight(); 
    this.stopMovingUp(); 
    this.stopMovingUpRight(); 
    this.stopMovingUpLeft(); 
    this.stopMovingDownLeft(); 
    this.stopMovingDownRight(); 
    this.isMovingDown=true;
}


public BetaMonster(){
    this.hp=10;
    this.frame=0;
    this.attack=1;
    this.setSpeed(2f);
    this.setSprite("res/Images/Sprites/Monster/MonsterSheet.png");
}

public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException {  

    if(levelMusic.playing()!=true){levelMusic.loop();}
    centerCharX=camX+screenWideOffset-(pocky.getSprite(0, 0).getScaledCopy(3f).getWidth()/2);//Find Center of Character 1
    centerCharY=camY+screenHighOffset-(pocky.getSprite(0, 0).getScaledCopy(3f).getHeight()/2);//Find Center of Character 1
    timer+=delta;//Timer
    controller.poll();//Poll Controller Buttons

    for(int f=0; f<spawnList.size(); f++){
            final Iterator<SpawnPoint> spawnIter = spawnList.iterator();
            while(spawnIter.hasNext()){
            final SpawnPoint spawn = spawnIter.next();
            if((timer/1000)>spawnCount){
            spawnCount+=5;
            BetaMonster ghost = new BetaMonster();
            ghost.setAtk(0);
            ghost.setCollidable(true);
            ghost.setHP(5);
            ghost.setX(spawn.getSpawnX());
            ghost.setY(spawn.getSpawnY());
            ghost.setWidth(32);
                ghost.setHeight(32);
            ghostList.add(ghost);
            }
        }
    }
    controlFlags(gc);//Check Keys and Flag Use
    scrollCam(gc);//Camera Movement Check from Control
    moveChar(gc);//Character Movement
    checkPlayerMoving();//Check Player's Movement with Animations
    projectilePhysics();//Checks Projectile Physics
    if(collideThread.isAlive()==false){collideThread.run();}//Ensure Thread is Running
    collideThread.checkCollision(timer, playerProjectiles, player, tileList); //Check Projectiles against their time to Die. Must rename when can be arsed.
    collideThread.playerTileCollission(playerProjectiles, player, tileList);//Checks player-tile collision
    collideThread.checkTileProjectileCollision(playerProjectiles, player, tileList);//Checks projectile-tile collision
    collideThread.checkMonsterProjectileCollision(playerProjectiles, player, ghostList);//Checks monster-projectile collision
    for(int k = 0; k<ghostList.size(); k++){
        BetaMonster monsters = ghostList.get(k);
        Rectangle tempRect = new Rectangle(player.getX()-490, player.getY()-200, 64, 64);
        collideThread.monsterLogic(player, ghostList);
        if(monsters.getBoundingBox().intersects(tempRect)==true){}//TODO If Player and Monster Intersect, Do Damage.    
        if(monsters.getBoundingBox().intersects(tempRect)==true && defendKey==true){monsters.takeDamage(player.getDamage()*3);}
    }//End For Loop
}//end Update   

Apologies if my post needs more info, I'm new to stack overflow.


Solution

  • I found the error. It had to do with calling the ghost.update(); method in the iterator rather than as a part of the linkedlist.

    for(int k = 0; k<clickList.size(); k++){
    clickList.get(k).update();}