Can anyone help me access a value from one of my actors to use in another? I am basically making a game with a cannon and I have an actor that sets the angle of fire and and actor that sets the power of fire. Im trying to use the values from the two actors in other classes in calculations for gravity so the projectile knows how to act. Essentially the xSpeed and ySpeed. Here is some of the code.
**This is the code for my world.**
public class ProjectileWorld extends World
{
/**
* Constructor for objects of class ProjectileWorld.
*
*/
private double angle;
private double power;
public ProjectileWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
//Add new powerbar
PowerBar PB=new PowerBar(200);
double power=PB.getFullPercentage(); //ATTEMPTING TO ADD A POWERBAR TO THE WORLD AND GET ITS PERCENT VALUE
addObject(PB, 162, 592);
//Add new Anglearc
AngleArc AA= new AngleArc(50);
addObject(AA, 27, 572);
double angle=AA.getAngleRadians(); //ATTEMPTING TO ADD ANGLEARC TO THE WORLD AND GET ITS ANGLE IN RADIANS
//Add Static Circles
addObject(new Obstacle((Greenfoot.getRandomNumber(76)+25)),(Greenfoot.getRandomNumber(800)+100),Greenfoot.getRandomNumber(500));
addObject(new Obstacle((Greenfoot.getRandomNumber(76)+25)),(Greenfoot.getRandomNumber(800)+100),Greenfoot.getRandomNumber(500));
addObject(new Obstacle((Greenfoot.getRandomNumber(76)+25)),(Greenfoot.getRandomNumber(800)+100),Greenfoot.getRandomNumber(500));
addObject(new Obstacle((Greenfoot.getRandomNumber(76)+25)),(Greenfoot.getRandomNumber(800)+100),Greenfoot.getRandomNumber(500));
addObject(new Obstacle((Greenfoot.getRandomNumber(76)+25)),(Greenfoot.getRandomNumber(800)+100),Greenfoot.getRandomNumber(500));
//Add Projectile
addObject(new BallProjectile(),50,559);
}
public double getSpeedValue() //TRYING TO USE THIS METHOD TO CALCULATE BALL XSPEED,YSPEED
{
power=power*15.0;
return power;
}
public double getAngleValue() //TRYING TO USE THIS METHOD TO CALCULATE BALL XSPEED,YSPEED
{
return angle;
}
}
**This is the portion of the code I'm trying to access from the PowerBar class.**
/**
* getFullPercentage - calculate how full the PowerBar is
*
* @return the ratio (percentage) of fullness
*/
public double getFullPercentage()
{
return power/(double)maxPower;
}
**This is the portion of code I'm trying to access from the AngleArc class.**
/**
* getAngleRadians - returns how full the anglearc is
*
* @return the current fill amount in RADIANS
*/
public double getAngleRadians()
{
return Math.toRadians(power);
}
**This is the BallProjectile class I'm trying to use them in.**
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class BallProjectile here.
*
*
*
*/
public class BallProjectile extends Actor
{
private int xSpeed;
private int ySpeed;
public BallProjectile()
{
xSpeed=0;
ySpeed=0;
setImage(new GreenfootImage(25, 25));
getImage().setColor(Color.blue);
getImage().drawOval(0,0,25,25);
getImage().fillOval(0,0,25,25);
}
/**
* Act - do whatever the BallProjectile wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
ProjectileWorld w=(ProjectileWorld)getWorld();
if(Greenfoot.isKeyDown("space")){
xSpeed =(int)(w.getSpeedValue()*Math.cos(w.getAngleValue())); //THIS IS THE LINE IM TRYING TO USE FOR XSPEED WITH GRAVITY
ySpeed = -(int)(w.getSpeedValue()*Math.sin(w.getAngleValue()));//THIS IS THE LINE IM TRYING TO USE FOR YSPEED WITH GRAVITY
setLocation (getX() + xSpeed, getY() - ySpeed);}
if(getY()<=0){
setImage(new GreenfootImage(25,25));
getImage().setColor(Color.white);
getImage().drawOval(0,0,25,25);
getImage().fillOval(0,0,25,25);}
if (getX()>w.getWidth()-2){
setImage(new GreenfootImage(25,25));
getImage().setColor(Color.white);
getImage().drawOval(0,0,25,25);
getImage().fillOval(0,0,25,25);
}
if (getY() >= w.getHeight()-1) {
setImage(new GreenfootImage(40, 20));
getImage().setColor(Color.magenta);
getImage().drawOval(0,0,40,20);
getImage().fillOval(0,0,40,20);
}
} }
