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

2017/6/15

How to spawn something like a firebolt out from a character in any of the cardinal directions?

Sirpancakeking Sirpancakeking

2017/6/15

#
Curious on how I can spawn something in front of the main class that will essentially just shoot a big moving fireball, similar to a bullet but it spawning in front or out from the main class which I have named Gnaa. Also if anyone can help me with the first part, I would also like the firebolt to be shot at a steady pace with it coming out say once every 3 seconds (the only way I have tried figuring this part out is with timers but I still don't know how to do that perfectly so if anyone knows how it can shoot something every 3 seconds that would be great).
danpost danpost

2017/6/15

#
I presume that this would be a top-down shooter where the rotation of the man is set to 0, 90, 180 or 270 depending on key presses. That being said, the direction of the fireballs should be set to the current facing direction of the man at the time the fireball is created. For the "every three seconds" part, you will need an int timer field to count act frames. When it reaches some value probably around 165 or 180, then create a fireball and reset the timer back to zero.
Sirpancakeking Sirpancakeking

2017/6/15

#
Okay, so I understand what you mean but my problem is that I don't know the code to put to make it so when a key is pressed say, to spawn a firebolt class possibly then figuring out how to spawn it say 20 units in front of whatever direction i'm facing.
danpost danpost

2017/6/15

#
Oh, not automatic fire -- but triggered fire. In that case I would have the timer set to the high value when a shot is fired and decrement it in the act method when needed. Then only fire when key is pressed AND the timer value is zero. Spawn the fireball at the location of the player, set its rotation and move it the required amount (20 units) when firing.
Sirpancakeking Sirpancakeking

2017/6/15

#
Okay, so I understand the way I would go about it. The only problem is I don't know how I would code that. So I am asking, what would i type in my code so I can spawn something that is in relation to a class. Because all the other spawn codes ive seen spawn it at a set location on the map, as opposed to in relation to a class's location which is what im asking for.
Super_Hippo Super_Hippo

2017/6/15

#
1
2
3
4
Actor fireball = new Fireball(); //create a new fireball
getWorld().addObject(fireball, getX(), getY()); //add the fireball at the same position as the one who shoots
fireball.setRotation(getRotation()); //set the rotation of the shooter to the fireball
fireball.move(20); //move the fireball 20 cells in that direction
Sirpancakeking Sirpancakeking

2017/6/16

#
Sorry, I forgot to mention that my character moves around as you control him, so what I was trying to figure out was how to spawn the fireball wherever my character is in the level, as if he was a walking mage shooting fireballs out of his staff
Super_Hippo Super_Hippo

2017/6/16

#
The code I gave you should be placed in the act method of the one which shoots the fireballs, so the mage.
1
2
3
4
5
6
7
8
9
public void act()
{
    //move around
    //some condition for shooting, for example:
    if (Greenfoot.isKeyDown("space") && <some timer is zero to prevent multiple shots at once>)
    {
        //code from above
    }
}
getX and getY takes the position of the mage which will then be used as coordinates for the fireball.
You need to login to post a reply.