This site requires JavaScript, please enable it in your browser!
Greenfoot back
horde
horde wrote ...

2017/3/16

Blast animation

horde horde

2017/3/16

#
Hello, Started coding a game, so far so good. However, I face a minor problem, which I cannot solve by myself: When I use BLAST my character (actor) gets out of his position. Here's GIF: https://giphy.com/gifs/xUA7b7mnhsPWCcgCc0 My blast code is very simple (just using .gif image of bast):
if(Greenfoot.isKeyDown("shift") && !(Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("left"))) {
                setImage(newCharBlast.getCurrentImage());
        }
If I understand correctly, blast image gets centered thus making it look like this. Any solutions (if possible with code)?
danpost danpost

2017/3/16

#
The location of the actor is not changing. It is just that the width of its image has changed so dramatically that the character is no longer centered in the image but at the far left of a much wider one. I guess you will have to move the character forward some whenever the blast is running. Something like:
int normalImageWidth = 40; // adjust as needed
move(-(getImage().getWidth()-normalImageWidth())/2);  // undo any previous move
setImage(newCharBlast.getCurrentImage()); // set new image
move((getImage().getWidth()-normalImageWidth())/2); // adjust to new image
horde horde

2017/3/16

#
While waiting, I tried to make something happen by myself and I actually think this would be ?better? So I seperated that gif into 2 pieces: Hero animation and Blast animation Here u can see Hero animation: https://giphy.com/gifs/3og0IwQx31I3VQ9IIg So I need to add Blast.gif in the right position. Current code for both directions animation:
if(Greenfoot.isKeyDown("shift") && direction == 1) {
            setImage(newCharBlast.getCurrentImage());
        }
        if(Greenfoot.isKeyDown("shift") && direction == -1) {
            setImage(newCharBlastL.getCurrentImage());
        }
Suggestions?
danpost danpost

2017/3/16

#
If you separated the two, then you need a separate actor to display the blast gifs and that actor needs to be placed properly ahead of the character. The character can keep a reference to that actor so it can remove it when the shift is not down and it is still in the world or for when the character changes direction. The you just supplied should then only add the blast actor at the appropriate locations in the world. The blast actor can take care of running the gifs on itself.
horde horde

2017/3/16

#
May you give me an example? Appreciate your fast responses!
danpost danpost

2017/3/16

#
horde wrote...
May you give me an example? Appreciate your fast responses!
Just create two subclasses of actor and name them BlastR and BlastL. Add the appropriate animation codes into each one. Have the character create and keep a reference to one object of each class for it to control. Have the character add the appropriate one into the world when needed and remove from the world any that need removed.
horde horde

2017/3/16

#
so I went back to your first suggestion and it works pretty much. There's just 1 thing, doesn't work same on left as right Here's gif: https://giphy.com/gifs/l4FGAlDs0gjMDpTbi Current code:
if (Greenfoot.isKeyDown("left")) {  
            moveLeft(); 
            int normalImageWidth = 70; // adjust as needed
            move(-(getImage().getWidth()-normalImageWidth)/2);  // undo any previous move
            setImage(newChar2.getCurrentImage()); // set new image
            move((getImage().getWidth()-normalImageWidth)/2); // adjust to new image
            direction = -1;
        }
        if(Greenfoot.isKeyDown("right")) {
            moveRight();
            int normalImageWidth = 70; // adjust as needed
            move(-(getImage().getWidth()-normalImageWidth)/2);  // undo any previous move
            setImage(newChar.getCurrentImage()); // set new image
            move((getImage().getWidth()-normalImageWidth)/2); // adjust to new image
            direction = 1;
        }
        if(Greenfoot.isKeyDown("shift") && direction == 1) {
            int normalImageWidth = 70; // adjust as needed
            move(-(getImage().getWidth()-normalImageWidth)/2);  // undo any previous move
            setImage(newBlast.getCurrentImage()); // set new image
            move((getImage().getWidth()-normalImageWidth)/2); // adjust to new image
        }
        if(Greenfoot.isKeyDown("shift") && direction == -1) {
            int normalImageWidth = 10; // adjust as needed
            move(-(getImage().getWidth()-normalImageWidth)/2);  // undo any previous move
            setImage(newBlastL.getCurrentImage()); // set new image
            move((getImage().getWidth()-normalImageWidth)/2); // adjust to new image(newCharBlastL.getCurrentImage());
        }
danpost danpost

2017/3/16

#
With left, you will have to reverse the signs in the movement codes (lines 4 and 6) so that it is basically the negative value of the respective right move values.
horde horde

2017/3/16

#
Worked, thanks. Yet another problem: When I move to opposite direction after shooting my Char's position gets changed Demo: https://giphy.com/gifs/3og0IKVuC4EubwgW64 and Code:
if (Greenfoot.isKeyDown("left")) {  
            moveLeft(); 
            int normalImageWidth = 70; // adjust as needed
            move((getImage().getWidth()+normalImageWidth)/2);  // undo any previous move
            setImage(newChar2.getCurrentImage()); // set new image
            move(-(getImage().getWidth()+normalImageWidth)/2); // adjust to new image
            direction = -1;
        }
        if(Greenfoot.isKeyDown("right")) {
            moveRight();
            int normalImageWidth = 70; // adjust as needed
            move(-(getImage().getWidth()-normalImageWidth)/2);  // undo any previous move
            setImage(newChar.getCurrentImage()); // set new image
            move((getImage().getWidth()-normalImageWidth)/2); // adjust to new image
            direction = 1;
        }
        if(Greenfoot.isKeyDown("shift") && direction == 1) {
            int normalImageWidth = 70; // adjust as needed
            move(-(getImage().getWidth()-normalImageWidth)/2);  // undo any previous move
            setImage(newBlast.getCurrentImage()); // set new image
            move((getImage().getWidth()-normalImageWidth)/2); // adjust to new image
        }
        if(Greenfoot.isKeyDown("shift") && direction == -1) {
            int normalImageWidth = 10; // adjust as needed
            move((getImage().getWidth()+normalImageWidth)/2);  // undo any previous move
            setImage(newBlastL.getCurrentImage()); // set new image
            move(-(getImage().getWidth()+normalImageWidth)/2); // adjust to new image(newCharBlastL.getCurrentImage());
        }
danpost danpost

2017/3/16

#
Well, the left and right gif actors should be placed at different location ... the left to the left side of the character and the right to the right.
horde horde

2017/3/16

#
How
danpost danpost

2017/3/16

#
horde wrote...
How
I cannot say without seeing your revised 'shift' detection codes.
horde horde

2017/3/16

#
if (Greenfoot.isKeyDown("left")) {  
            moveLeft(); 
            int normalImageWidth = 70; // adjust as needed
            move((getImage().getWidth()+normalImageWidth)/2);  // undo any previous move
            setImage(newChar2.getCurrentImage()); // set new image
            move(-(getImage().getWidth()+normalImageWidth)/2); // adjust to new image
            direction = -1;
        }
        if(Greenfoot.isKeyDown("right")) {
            moveRight();
            int normalImageWidth = 70; // adjust as needed
            move(-(getImage().getWidth()-normalImageWidth)/2);  // undo any previous move
            setImage(newChar.getCurrentImage()); // set new image
            move((getImage().getWidth()-normalImageWidth)/2); // adjust to new image
            direction = 1;
        }
        if(Greenfoot.isKeyDown("shift") && direction == 1) {
            int normalImageWidth = 70; // adjust as needed
            move(-(getImage().getWidth()-normalImageWidth)/2);  // undo any previous move
            setImage(newBlast.getCurrentImage()); // set new image
            move((getImage().getWidth()-normalImageWidth)/2); // adjust to new image
        }
        if(Greenfoot.isKeyDown("shift") && direction == -1) {
            int normalImageWidth = 10; // adjust as needed
            move((getImage().getWidth()+normalImageWidth)/2);  // undo any previous move
            setImage(newBlastL.getCurrentImage()); // set new image
            move(-(getImage().getWidth()+normalImageWidth)/2); // adjust to new image(newCharBlastL.getCurrentImage());
        }
danpost danpost

2017/3/16

#
I think the distance moved has to be the differences in the width -- not plus or minus the sum of the width (for one thing). The normal width looks wrong at line 24 (for another).
You need to login to post a reply.