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

2014/11/3

Accessing a Value from an actor to be used in another method

1
2
KG.1987 KG.1987

2014/11/3

#
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); } } }
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
If you're just trying to make variables that can be called from a differend class you should be able to call then with the following:
classname name = new classname();
name.variablename;
classname is the name of the class you're trying to get the variable from. Name is the new reference to the class you're trying to get the variable from and variablename would be the name of the variable you're trying to get. There is however one thing you have to remember when doing this. You have to make sure that the variable is declared in a certain position in your code which is at least outside of any of your methods and usually at the top to make a code easier to read. So like this:
public class projectileworld extends world
{
   int variable;

  public void act()
  {

  }
}
KG.1987 KG.1987

2014/11/4

#
So if I'm trying to access the getSpeedValue() method to use it in the actor class BallProjectile() do I need to create a ProjectileWorld? Such as: ProjectileWorld w=new ProjectileWorld(); Then: w.getSpeedValue();
Alwin_Gerrits Alwin_Gerrits

2014/11/4

#
It doesn't work i presume? If you want me to help you have to be clear about the problem. You asked :
Can anyone help me access a value from one of my actors to use in another?
So I simply responded by giving you this idea. Seen you're allready using it doesn't that mean you're asking the wrong question? Even if it doesn't work then poste it doesn't instead of asking how it should work straigth away.....
Alwin_Gerrits Alwin_Gerrits

2014/11/4

#
On the other side yes, maybe changing ProjectileWorld w=(ProjectileWorld)getWorld(); by what i proposed would solve the problem. Just try, why not.
KG.1987 KG.1987

2014/11/4

#
yeah, sorry some of my word usage was incorrect. I just need to get a value from getFullPercentage() method in the PowerBar() class which is used in the getSpeedValue() method in the ProjectileWorld() class. I then need to use the return value from the getSpeedValue() method and use it in my act() method in the BallProjectile() class. Sorry still pretty new to this.
Alwin_Gerrits Alwin_Gerrits

2014/11/4

#
It's okay, give me some time to take a look at it. By the way, there is a code button underneath the content bar underneath post a reply... if you just put your code in there it will appear as a reall code on the forum instead of just some text. Maybe a tip for next time?
Alwin_Gerrits Alwin_Gerrits

2014/11/4

#
K, I think i get the idea. It seems to be prety wel coded on first sight. I have one thing that might help though. try replacing
xSpeed =(int)(w.getSpeedValue()*Math.cos(w.getAngleValue()));
by
xSpeed=w.getSpeedValue()*Math.cos(w.getAngleValue()));
if that doesn't help or gives errors just change it back and let me know. Allso it would have if you pointed out if you're getting an error or the code just doesn't do what it's supposed to do. That helps with finding the problem see?
KG.1987 KG.1987

2014/11/4

#
The code as shown doesn't give an error, but nothing happens when the "space bar" is pressed. Im under the assumption that when creating a PowerBar in the ProjectileWorld() constructor the getFullPercentage() method is getting the initial value which is set to zero when the PowerBar is created. Therefore the projectile is not moving because its always set to zero. I tried creating the PowerBar in the constructor and using the getFullPercentage() method in the getSpeedValue() method but that causes an error. The code will compile but causes an error when you try to run it. Thanks for the tips, I'll put any other code in the correct format next time.
danpost danpost

2014/11/4

#
The following is assuming that you always have one and only one PowerBar object in the world at any time. First, remove the 'double power= ...' and 'double angle= ...' from your world constructor (where you have '//ATTEMPTING TO...'). Then remove the 'getSpeedValue' and 'getAngleValue' methods from the same class. In the act method of the BallProjectile class, use the following
KG.1987 KG.1987

2014/11/4

#
Oh, and the xSpeed has to be type cast (int) because its expecting an int and the value is a double
danpost danpost

2014/11/4

#
The following is assuming that you always have one and only one PowerBar object in the world at any time. First, remove the 'double power= ...' and 'double angle= ...' from your world constructor (where you have '//ATTEMPTING TO...'). Then remove the 'getSpeedValue' and 'getAngleValue' methods from the same class. In the act method of the BallProjectile class, use the following
double percentage = ((PowerBar)getObjects(PowerBar.class).get(0)).getFullPercentage();
Reposted to show the code (which was incorrectly tagged).
KG.1987 KG.1987

2014/11/4

#
Unfortunately I'm required to keep the getSpeedValue() and getAngleValue() methods in the ProjectileWorld() class.
Alwin_Gerrits Alwin_Gerrits

2014/11/4

#
Uuh you do know it's space not spacebar right? the spacebar button shouldn't exist... if i'm correct
Alwin_Gerrits Alwin_Gerrits

2014/11/4

#
Allso you might try putting in some System.out.println("anything you want here"); statements. That way you can see if everything is called correctly
There are more replies on the next page.
1
2