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

2020/4/14

Accessing speed from another class and adding to it

DaRafster DaRafster

2020/4/14

#
In my speed boost class, I'm trying to make it so when it touches a magnet projectile, it will access my spacecraft class and add +1 speed to it. However I'm unsure of how to perform this task. Here is my failed attempt: Here is my speed class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Speed here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Speed extends Powerups
{
    int magnetEffectSpeed = 3;
    int moveSpeed = 5;
    public Speed()
    {
        //divide by width, divide by height, set rotation
        customAdjustments(10,10,0); 
    }
    
    public void act() 
    {
        move(magnetEffectSpeed);
        magnetEffect();
        movePowerup(moveSpeed);
        removingPowerup();
    }        
    
    public void addingSpeed()
    {
        if(isTouching(Magnet.class))
        {
            int speed = Spacecraft.getSpeed();
            speed++;
        }
    }
}
Here is my spacecraft class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Spacecraft here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spacecraft extends Actor
{
    boolean canFire = true;
    private int speedCounter;
    public static int speed_ = 3;
    
    private final static int FB_DELAY = 45;
    private final static int MG_DELAY = 150;
    private int fbCounter_, mgCounter_;
    public Spacecraft()
    {
        GreenfootImage img = getImage();
        img.scale(img.getWidth()/3,img.getHeight()/3);
        setImage(img);
        setRotation(90);
    }
    
    public void act() 
    {
        moveAround();
        processFireball();
        processMagnet();
        collectBoost();
    }   

    public void moveAround()
    {
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-speed_);
        }
        else if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+speed_);
        }
    }
    
    public void processFireball()
    {
        if(Greenfoot.isKeyDown("f"))
        {
            if(fbCounter_ % FB_DELAY == 0)
            {
                Fireball shootThis = new Fireball(-10);
                getWorld().addObject( shootThis, getX() + 45, getY() );
            }
            fbCounter_++;
        }
    }
    
    public void processMagnet()
    {
        if(Greenfoot.isKeyDown("m"))
        {
            if(mgCounter_ % MG_DELAY == 0)
            {
                Magnet shootThis = new Magnet(-3);
                getWorld().addObject( shootThis, getX() + 45, getY() );
            }
            mgCounter_++;
        }
    }
    
    public void collectBoost()
    {
        if (isTouching(Speed.class))
        {
            speed_++;
            removeTouching(Speed.class);
        }
    }
    
    public static int getSpeed()
    {
        return speed_;
    }
    
    public int getSpeed2()
    {
        return speed_;
    }
}
So how would I add speed to the spacecraft when the speed class touches the magnet class?
DaRafster DaRafster

2020/4/14

#
Also can someone explain to me what static does?
danpost danpost

2020/4/14

#
"static" indicates that the field or method belongs to the class itself. That is, a static field is not a state of any object instance created from the class and a static method does not describe any behavior of an object instance created from the class. Non-static fields are considered "instance fields" and describe states of an instance. Each instance created from the class will have its own field with its own particular value. Non-static methods are considered "instance methods" and describe behaviors of an instance. You cannot access a non-static field or method without a reference to a particular instance as it would be the state or behavior of that instance you would be working with. Non-static fields are only created when an instance of the class is created. Static fields are considered "class fields" and, likewise, static methods are considered "class methods". Static fields, as single fields, and methods are shared among all instances of the class. These can be considered global content and may not directly involve the instances. Both can be accessed by the class name or by referencing an instance of the class, which is less common. Static fields are created during the compiling of the program. They DO NOT reset when you reset a scenario.
DaRafster DaRafster

2020/4/14

#
danpost wrote...
"static" indicates that the field or method belongs to the class itself. That is, a static field is not a state of any object instance created from the class and a static method does not describe any behavior of an object instance created from the class. Non-static fields are considered "instance fields" and describe states of an instance. Each instance created from the class will have its own field with its own particular value. Non-static methods are considered "instance methods" and describe behaviors of an instance. You cannot access a non-static field or method without a reference to a particular instance as it would be the state or behavior of that instance you would be working with. Non-static fields are only created when an instance of the class is created. Static fields are considered "class fields" and, likewise, static methods are considered "class methods". Static fields, as single fields, and methods are shared among all instances of the class. These can be considered global content and may not directly involve the instances. Both can be accessed by the class name or by referencing an instance of the class, which is less common. Static fields are created during the compiling of the program. They DO NOT reset when you reset a scenario.
Thanks, I'm starting to get the idea. I feel overwhelmed by the amount of information I just received, so I need to let it sink in. Also, did you see my question before the static question, about my game? You've been very helpful, and I would like to know how to add speed to the spacecraft when the speed class touches the magnet class. If you answered this question through the static explanation, sorry, I'm not very good at applying new information I just received. I appreciate the help!
danpost danpost

2020/4/14

#
DaRafster wrote...
how to add speed to the spacecraft when the speed class touches the magnet class.!
Not previously answered.
Spacecaft.speed_++;
You need to login to post a reply.