Question

This is about animation in JAVA. I had success when using same dimension on all picture. But if i keep all picture dimension on same size(width and height) i get some bug which when player punch. Before player's hand touch enemy body, enemy died

But others with my case where idle, run, and punch has an different dimension. Punching animation facing to the left became very strange. Should his hand hit to the left but his body shifts to the right. This is because I draw on the x & y are the same.

How can I fix it? Need instructions :D

I use png coz support transparent

I think this can fix with 2 option 1. Fix my collision detection 2. Fix of drawing position my image when some condition occur

Was it helpful?

Solution

Trying to picture your problem, hope this helps. I am typeing directly from my head, so there are might errors in code

  1. fixing coalision decection i would try this

     Image fist
     Image enemy
        //in paint
    
    g2D.drawImage(fist,x,y,this);
    
    g2D.drawImage(enemy,x1,y1,this);
    
    Rectangle2D myFist = new Rectangle2D.Double(x,y,fist.getWidth(this),fist.getHeight(this));
    Rectangle2D myEnemy = new Rectangle2D.Double(x1,y1,enemy.getWidth(this),enemy.getHeight(this));
    if (myEnemy.contains(myFist){
    //action u want to happend
    }
    

I think something like this should fix coalision problems I am seeing this as mario a game on sega

  1. Fix of drawing position

    //arm image can be the same image if u want
        Image leftArm;
        Image rightArm;
        image headLegsAndTorsoLeft;
        image headLegsAndTorsoRight;
        //where am i looking in game if true i look to the leftside of user thats playing
        boolean turnedLeft
        //in paint
        if(turnedLeft){
        //this lets it look like he is turned to the left with his right arm in the visible behind his left.
        //draw right arm
        g2D.drawImage(rightArm,x,y,this);
        //draw body moved a bit in x coor
                    g2D.drawImage(headLegsAndTorsoLeft,x-3,y,this);
       // draw left arm a bit more in x coor
        g2D.drawImage(leftArm,x-6,y,this);
        }else{
         //this lets it look like he is turned to the right with his left arm in the visible behind his right.
        // draw left arm
            g2D.drawImage(leftArm,x,y,this);
         //draw body moved a bit in x coor
            g2D.drawImage(headLegsAndTorsoRight,x-3,y,this);
     //draw right arm a bit more in x coor
            g2D.drawImage(rightArm,x-6,y,this);
            }
    

    same order for animation of arms, ultimatly i would use different methods animations for torso, leftarm, rightarm something like keypressed leftarrow torso does walking animation left, hit left arm key moves left arm,hit right arm key moves right arm,thats 3 for lets say left arm, now u need another 3 for when ur char is moved to the right.

    Thats how i would try to do things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top