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

2013/5/12

coding problem

1
2
Banginwithflavor Banginwithflavor

2013/5/12

#
How would i take the getX() of one object and apply it to another object? Also how would i add an object to the world after a condition has been met?
danpost danpost

2013/5/12

#
if (condition) getWorld().addObject(new ObjName(), getX(), getY());
Kartoffelbrot Kartoffelbrot

2013/5/12

#
No chance to comment. danpost is always fastest. :)
Banginwithflavor Banginwithflavor

2013/5/12

#
public void checkTarget()
    {
        if(Greenfoot.isKeyDown("z"))
        {
          getWorld().addObject (new laser(),300, 165);
        }
hmm. This was my code but when i try to run it nothing occurs. And where the 300 is i want to set that equal to an object that is already in the world. Do i need to set the other objects x = to a variable then out that where 300 is?
danpost danpost

2013/5/12

#
(1) make sure you can instantiate a laser object by right-clicking and selecting 'new laser()' from the options and then clicking anywhere in the world area (2) make sure that the 'checkTarget' method is being called somewhere within the 'act' method or a method it calls (3) you will probably need to add a delay between laser bursts
Banginwithflavor Banginwithflavor

2013/5/12

#
hmm i cant seem to get this to work. i have called the method and i can place a laser in the world but it doesnt appear when i try this.
danpost danpost

2013/5/12

#
Banginwithflavor wrote...
hmm i cant seem to get this to work. i have called the method and i can place a laser in the world but it doesnt appear when i try this.
Does this mean: step 1: passed step 2: passed but, still no laser objects appear during the running of the simulation while the z key is pressed?
Banginwithflavor Banginwithflavor

2013/5/12

#
yes :D
danpost danpost

2013/5/12

#
Ok. Post the class that 'fires' the laser.
Gevater_Tod4711 Gevater_Tod4711

2013/5/12

#
The act method of an object is only called if it is in the world. Maybe thats the problem. Are you shure that the object that calls the method is in the world when you press the key?
danpost danpost

2013/5/12

#
Make sure your 'Caps Lock' is off.
Banginwithflavor Banginwithflavor

2013/5/12

#
ah i think i might not have explained myself clearly. Im not shooting lasers. Im trying to make a sort of "orbital cannon" i want the player to be able to move the "target" to the location they want and i want the laser to take the X coordinate of the target and use it as its own. i dont have a fireLaser() class
Banginwithflavor Banginwithflavor

2013/5/12

#
the object i am calling isnt in the world. Im trying to add it to the world.
danpost danpost

2013/5/12

#
Banginwithflavor wrote...
public void checkTarget()
{
    if(Greenfoot.isKeyDown("z"))
    {
      getWorld().addObject (new laser(),300, 165);
    }
In what class did you place this code? Is it a world class or an actor class? and what is the name of that class?
Banginwithflavor Banginwithflavor

2013/5/12

#
ive tried both but it is currently in an actor class
There are more replies on the next page.
1
2