0

I'm currently having an issue where the image won't move when the y-value of the image is increasing. I wanted the image to move vertically so I made a method that controls the speed of the image, The parameter is the ActionEvent and the y-value for the player. The y-value increases as I printed out the value, but the image won't move. I've even added the timer which didn't make the image move at all. No error in the output whatsoever.

 public void speedPerformed(ActionEvent e, Rectangle movingPlayer){
        int speed = 15;
        // help deals with gravity
        ticks++;
        
         
        // if the remainder of zero is given from tick
        //then the object will feel heavier
        if (ticks % 2 == 0 && yMotion < 15) {
            yMotion += 3;
        
        }
        movingPlayer.y += yMotion;
       
    }


 @Override
    public void actionPerformed(ActionEvent e) {
        
        action.speedPerformed(e,movingPlayer);
        gameInterface.getRenderer().repaint();
       
        
    }

public void repaint(Graphics g) {
 
       g.drawImage(Renderer.renderer.getImage(),movingPlayer.x,movingPlayer.y,100,100, null);
    
    }




public RepaintConfiguration(GameInterface gameInterface) {
      this.gameInterface = gameInterface;
      lasers = new Lasers();
      action = new Actions();
      movingPlayer = new Rectangle(0, 550, 100, 100);
      timer = new Timer(20,this);
      timer.start();
    }

let me know if you want to take a look at any of my codes that I haven't included here.

  • Still no idea why the structure of the code is so complicated. As was stated in your last question you should just do the game rendering in your game panel. The state of each object you want to paint is updated when the Timer is invoked and then the game panel is repainted. See: https://stackoverflow.com/questions/54028090/get-width-and-height-of-jpanel-outside-of-the-class/54028681#54028681 for a basic example. – camickr Jun 12 '21 at 17:25
  • Will it help me solve my problem of an image not being able to move? – JayRobbinsz Jun 12 '21 at 17:38
  • For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 12 '21 at 19:00
  • So what happened when you tested the code? Did you see objects moving around the panel? – camickr Jun 12 '21 at 19:55
  • Actually.. I fixed the problem. – JayRobbinsz Jun 12 '21 at 20:53

1 Answers1

0

You code is wrong: you are updating the local variable y.

You should update directly the movingPlayer:

public void speedPerformed(ActionEvent e){ int speed = 15; // help deals with gravity ticks++;

    // the y value of the moving player
    
    // if the remainder of zero is given from tick
    //then the object will feel heavier
    if (ticks % 2 == 0 && yMotion < 15) {
        yMotion += 3;
    
    }
    movingPlayer.y += yMotion;
   
}
NoDataFound
  • 11,381
  • 33
  • 59