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

2011/10/27

Getting My Character To Flip 360 Degrees

1
2
metaleddie13 metaleddie13

2011/10/27

#
I am having trouble getting my character to do a full left or right flip, depending on which is pressed. I tried setLocation(getX(), getY(); but I just could not have it work. Is there anyone who can help?
    public void flipLeft()
    {
        flip(-360);       
    }
    
    public void flipRight()
    {
        flip(360);    
    }    
    
    private void flip(int x)
    {
        
    } 
danpost danpost

2011/10/27

#
To actually see an object flip, you need to do the flip by rotating in small increments. Set up a flag and a counter in the actor to track if flipping is activated and how far it has progressed. Then each act() of the object can check the flag, and if the object is flipping, flip a small amount more, and adjust counter, checking the counter to see if the flip is complete, to turn the flag off. By the way, you will also need an integer variable to store the increment value (positive for right flip/negative for left).
metaleddie13 metaleddie13

2011/10/27

#
So could I set up a for loop in flip somehow? I'm just fried from messing with this thing for hours.
nccb nccb

2011/10/27

#
When you say flip, do you want the image to flip, or do you want the position of the actor to flip, or do you want the direction of the actor to flip (or some combination of the three)?
metaleddie13 metaleddie13

2011/10/28

#
I need the image to rotate 360 degrees (left or right) and land on original position.
metaleddie13 metaleddie13

2011/10/28

#
I tried getRotation(setRotation() + 10); but it only moved 10 degrees in one direction. I have to pass a negative number for flip() to do a left flip and a positive number for right flip() to do a right flip.
ez4u2c ez4u2c

2011/10/28

#
// declare flip in your actor and assign it +/-1 somehow when you want the actor to flip
private int flip = 0; // or 1 or -1 (positive to flip left, negative to flip right)
// in the act method you could put something like this:
If (flip !=0) {
   setRotation(getRotation() + flip);  // if flip active, actor rotates by 1 degree each act iteration
   if (getRotation() == 0) flip = 0;  // if back to zero degrees, turn off flip
}
ez4u2c ez4u2c

2011/10/28

#
I just noticed that Greenfoot rotation is non-standard (positive is clockwise instead of counter-clockwise), so it's opposite what I said above.
Builderboy2005 Builderboy2005

2011/10/28

#
Technically it *is* standard, it's just that the standard for computers is different from the standard for mathematics.
nccb nccb

2011/10/28

#
@ez4u2c The rotation comes from the axes switching in computing. In both standard maths and in Greenfoot, 0 degrees rotation in a unit circle will be the point (1, 0), whereas 90 degrees rotation will be the point (0, 1). The difference being that in maths, the latter point will be upwards (and therefore rotation goes anti-clockwise), whereas on screen in Greenfoot, the latter point will be downwards (thereby making the rotation clockwise). As for the original poster, why do you want to flip 360? The code:
turn(360);
technically does that, but you won't notice anything happening because turning 360 degrees is equivalent to doing nothing. If you want to animate it, danpost or ez4u2c's suggestions are along the right lines.
metaleddie13 metaleddie13

2011/10/28

#
Your right, but I have to see it for the effect of the character doing a flip.
metaleddie13 metaleddie13

2011/10/28

#
I am trying the codes but nothing is working.
metaleddie13 metaleddie13

2011/10/28

#
I am trying the codes but nothing is working.
ez4u2c ez4u2c

2011/10/28

#
You might consider posting the portion of code that's giving you trouble so someone can help. "Nothing is working" isn't much to go on.
metaleddie13 metaleddie13

2011/10/28

#
Your right.
// File header information places here

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.*;

public class JediNinjaViking  extends Actor
{
    // Instance Methods
    
    private boolean tired = false;
    private boolean bored = true;
    private boolean saberOn = false;
    
    // Instance Methods
    
    public void setRandomLocation()
    {
        setLocation(Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight())); //When "r" is pressed, set a random location.
    }
    
    public void flipLeft()
    {
        flip(-1);       
    }
    
    public void flipRight()
    {
        flip(1);
    }    
    
    private void flip(int x)
    {
        int flip = 0;
        for (int i = 1; i <= 8; i++)
        {
            if(flip > 0)
            {
            setRotation(getRotation() + 45);
            wait(10);
            }
            if (flip < 0)
            {
            setRotation(getRotation() - 45);
            wait(10);
            }  
    }
    }
    public void saberOn()
    {
        setImage("jedininja1.png"); // If "o" is pressed, switch images to appear as though light saber is turning on.       
        wait(1); 
        setImage("jedininja2.png");            
        wait(1); 
        setImage("jedininja3.png");            
        wait(1); 
        setImage("jedininja4.png");            
        wait(1); 
        setImage("jedininja5.png");            
        wait(1); 
        Greenfoot.playSound("1saberup.wav");
    }  
    public void saberOff()
    {     
        setImage("jedininja5.png"); // If "p" is pressed, switch images to appear as though light saber is turning off.
        wait(1); 
        setImage("jedininja4.png");            
        wait(1); 
        setImage("jedininja3.png");            
        wait(1); 
        setImage("jedininja2.png");            
        wait(1); 
        setImage("jedininja1.png");            
        wait(1); 
        Greenfoot.playSound("slurp.wav");
    }    
        
    public void sleep()
    {
        setImage("sleep1.png"); // If "s" is pressed, swich images to appear as though the Jedi is getting sleepy and falls asleep.          
        wait(1);
        setImage("sleep2.png");            
        wait(1);
        setImage("sleep1.png");            
        wait(1);
        setImage("sleep2.png");            
        wait(1);
        setImage("sleep1.png");            
        wait(1);
        setImage("sleep2.png");                                                      
    }
     
    
    public boolean isSleepy()
    {
        return tired;
    }
    
    public boolean isSaberOn()
    {
       return saberOn;
    }
       
    public boolean isBored()
    {
       return bored;
    }
    
    
    public void wait(int time)
    {
       Greenfoot.delay(10);
    }
}
There are more replies on the next page.
1
2