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:
Here is my spacecraft class:
So how would I add speed to the spacecraft when the speed class touches the magnet 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++;
}
}
}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_;
}
}

