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

2018/4/15

Atari Centipede Laser

CrossX105 CrossX105

2018/4/15

#
How do I make my actor object fire a laser beam when spacebar is pressed that continuously moves upwards like in the Atari Centipede game?
danpost danpost

2018/4/15

#
CrossX105 wrote...
How do I make my actor object fire a laser beam when spacebar is pressed that continuously moves upwards like in the Atari Centipede game?
What have you tried for an laser beam actor object and what have you tried as far as shooting them?
CrossX105 CrossX105

2018/4/16

#
Not much. Just naming an actor rocket as Laser and moves it to my Tux (basically the player who fires the lasers). import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Laser here. * * @author (your name) * @version (a version number or a date) */ public class Laser extends Actor { /** * Act - do whatever the Laser wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Laser() { GreenfootImage rocket = getImage(); rocket.scale(rocket.getWidth()/5, rocket.getHeight()/5); turn(-90); } public void act() { if(Greenfoot.isKeyDown("e")) { setLocation(Tux.getX(), Tux.getY()); } } }
danpost danpost

2018/4/16

#
Remove the code within the act method of the Laser class. The initial creation, rotation setting and placement into the world of a Laser object will all be done by the Tux object when the key is pressed. The laser should only move and remove itself when at world edge and maybe explode when it hits something (I do not know what all you plan on).
CrossX105 CrossX105

2018/4/17

#
How will the Laser's attributes (rotation, placement, etc.) be done by a different Actor? In this case, Tux?
danpost danpost

2018/4/17

#
CrossX105 wrote...
How will the Laser's attributes (rotation, placement, etc.) be done by a different Actor? In this case, Tux?
For example, you might have a method called fire that is launched by a method that checks conditions to fire that is called by the act method:
private void fire()
{
    Actor laser = new Laser(); // create laser
    laser.setRotation(getRotation()); // set rotation of laser
    getWorld().addObject(laser, getX(), getY()); // place laser into world
}
Do not take this as exact code you should use in your case. It is just an example of doing those things from an Actor subclass.
CrossX105 CrossX105

2018/4/19

#
Yeah My Laser already appears at a certain location. But during the scenario is playing, how do I address the Tux X and Y? Cuz I want the laser to reappear at the tux and move up etc.
danpost danpost

2018/4/19

#
CrossX105 wrote...
Yeah My Laser already appears at a certain location. But during the scenario is playing, how do I address the Tux X and Y? Cuz I want the laser to reappear at the tux and move up etc.
Have the Tux object create and add the laser into the world. You can set the rotation to 270 to have the laser move upward.
CrossX105 CrossX105

2018/4/20

#
So this way, I dont have to track the tux or find its location? It does it automatically. Ohhh...
CrossX105 CrossX105

2018/4/22

#
Thanks a lot! It worked, but now the laser doesn't move independently and only moves up when I HOLD the spacebar. As soon as i let go, the laser stops moving. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Laser here. * * @author (your name) * @version (a version number or a date) */ public class Laser extends Actor { /** * Act - do whatever the Laser wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Laser() { GreenfootImage rocket = getImage(); rocket.scale(rocket.getWidth()/5, rocket.getHeight()/5); turn(-90); } public void act() { int x = this.getX(); int y = this.getY(); if(this.isTouching(Mushrooms.class)){ removeTouching(Mushrooms.class); this.setLocation(x, 568); } if(Greenfoot.isKeyDown("left")) { setLocation(getX() - 3, getY()); } if(Greenfoot.isKeyDown("right")) { setLocation(getX() + 3, getY()); } if(Greenfoot.isKeyDown("space")){ if(!(isAtEdge() || isTouching(Mushrooms.class) || isTouching(Centipede.class))){ setLocation(getX(), getY() - 1); } } } }
danpost danpost

2018/4/22

#
What is the space key supposed to do?
CrossX105 CrossX105

2018/4/22

#
The Space key fires the laser when the player touches it.
danpost danpost

2018/4/22

#
CrossX105 wrote...
The Space key fires the laser when the player touches it.
Then why are you checking for "space" down in the Laser class. A laser should not create a laser -- it would be the rocket that does that.
You need to login to post a reply.