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

2012/8/19

First Game

WatchPatronus WatchPatronus

2012/8/19

#
I am new to greenfoot and am making my first game which is a fighting game with space invaders elements... how to I get my actors to shoot?
WatchPatronus WatchPatronus

2012/8/19

#
oops i forgot to include my code... enemy: public class enemy extends Actor { /** * Act - do whatever the enemy wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act () { moveAround(); } public void moveAround () { move(4); if (Greenfoot.getRandomNumber(100) < 10) { turn(Greenfoot.getRandomNumber(90) - 45); } if (getX() <= 5 || getY() >= getWorld().getWidth() -5) { turn(180); } if (getY() <= 5 || getY() >= getWorld().getHeight() - 5) { turn(180); } } } player1: public class person extends Actor { /** * Act - do whatever the person wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(-4); if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } // Add your action code here. } }
WatchPatronus WatchPatronus

2012/8/19

#
oops i forgot to include my code... enemy: public class enemy extends Actor { /** * Act - do whatever the enemy wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act () { moveAround(); } public void moveAround () { move(4); if (Greenfoot.getRandomNumber(100) < 10) { turn(Greenfoot.getRandomNumber(90) - 45); } if (getX() <= 5 || getY() >= getWorld().getWidth() -5) { turn(180); } if (getY() <= 5 || getY() >= getWorld().getHeight() - 5) { turn(180); } } } player1: public class person extends Actor { /** * Act - do whatever the person wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(-4); if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } // Add your action code here. } }
Gevater_Tod4711 Gevater_Tod4711

2012/8/20

#
Hi If you want your character to shoot you first need a new class to create an object which stands for the bullet. In this class you have to add the code that kills the enemy or makes the bullet move. Now your character has to add such an object to the world. Maybe if you press the space button. A possible code could be: if (Greenfoot.isKeyDown(“space”)) { getWorld().addObject(new Bullet(), getX(), getY()); } Now your character is able to add an object from type Bullet to the world. But it will only move to the right or left side. You need to add a constructor to the class Bullet in which you show the bullet object where to move: Code: //should be the first method in Bullet; public Bullet(int rotation) { setRotation(rotation); } Now you just have to add the rotation of your character: //add this in the act() method of your character if (Greenfoot.isKeyDown(“space”)) { getWorld().addObject(new Bullet(getRotation()), getX(), getY()); } Now your Bullet just has to kill the enemy. (if it finds one) Good luck with your game
GreenGoo GreenGoo

2012/8/23

#
I have done essentially the same in my game, but I can't find a way to regulate how many lasers are fired, so when I tap space I fire too many. Any help? Here is the relevant code, for the rocket and the laser: public class Laser extends Animal { public Laser(int direction) { setRotation(direction); } int direction = Greenfoot.getRandomNumber(360); public void act() { move(7); } } And this is my rocket: if (Greenfoot.isKeyDown("space")) { Greenfoot.playSound("Phaser.wav"); createLaser(); } And: private void createLaser() { Laser newLaser; newLaser = new Laser(getRotation()); World world = getWorld(); int x = getX(); int y = getY(); world.addObject(newLaser, x, y); }
danpost danpost

2012/8/23

#
This requires a dual-check. Add a boolean instance field called 'spaceDown' (boolean spaceDown = false;) to your player class. Then change the 'if(Greenfoot.isKeyDown("space"))' method to:
if (!spaceDown && Greenfoot.isKeyDown("space"))
{
    Greenfoot.playSound("Phaser.wav");
    createLaser();
    spaceDown = true;
}
if (spaceDown && !Greenfoot.isKeyDown("space")) spaceDown = false;
This will fire one shot each time the spacebar is pressed.
matthew12512 matthew12512

2012/9/1

#
I think you should start a little more simple. You will learn more as you get more experience in programming. My first game got over 100 posts. :)
DonaldDuck DonaldDuck

2012/9/1

#
danpost wrote...
This requires a dual-check. Add a boolean instance field called 'spaceDown' (boolean spaceDown = false;) to your player class.
Actually, it doesn't at all.
String key = Greenfoot.getKey();
if(key == null) { key = "null"; }
if(key.equals("spacebar")) { createLaser(); }
This will do the same thing as danpost's code.
danpost danpost

2012/9/1

#
@DonaldDuck, not exactly the same thing. My code creates the Laser object at the push of the spacebar while yours creates it at the release of the spacebar.
DonaldDuck DonaldDuck

2012/9/2

#
@danpost, true but it's a heck of a lot simpler (not that yours was complex code) and lets be honest, when firing lasers in Space Invaders, nobody really presses and holds the space bar, they just sorta mash it. It would appear to do the same thing anyways. The biggest up-side to your code is that you can extend it to check how long the space bar was depressed and charge the lasers power accordingly.
danpost danpost

2012/9/2

#
I think the 'feel' is better. You do not release a button to fire a missle, torpedo, bullet, or whatever. When timing is involved, it 'feels' better (from the users standpoint) to have the action happen at the press of the key. Also, if you use 'laserCharged' instead of 'spaceDown', the second 'if' part can be replaced with the charging code and the user can hold the spacebar to fire automatically when charged.
You need to login to post a reply.