Comparing coordinates for space invaders clone

thanks a lot! The buttons now work! I have updated my code, but now my problem is to compare the position of the bullet with the position of the ufo. I have marked the part where I wrote my solution, but it doesn't really work, do you have any suggestions?
Thanks.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Animation extends JPanel implements ActionListener{
    JButton button=new JButton("links");
    JButton button2=new JButton("rechts");
    int xpos=60;
    int xpos2=0;
    int height=15;
    int width=60;
    int dx=3;
    int dxshooter=3;
    int shoot=300;
    int ykogel=0;
    int dxkogel=6;
    int number=12;
    int xposkogel;
    int yposkogel;
    int ovalx1=xpos,ovalx2=xpos+100,ovalx3=xpos+200,ovalx4=xpos+300,ovalx5=xpos+400,ovalx6=xpos+500;
    int ovaly1=15,ovaly2=15,ovaly3=15,ovaly4=15,ovaly5=15,ovaly6=15;
    int ovalxb1=xpos+19,ovalxb2=xpos+19+100,ovalxb3=xpos+19+200,ovalxb4=xpos+19+300,ovalxb5=xpos+19+400,ovalxb6=xpos+19+500;
    int ovalyb1=5,ovalyb2=5,ovalyb3=5,ovalyb4=5,ovalyb5=5,ovalyb6=5;
    Timer fastTicker;
    Animation(){
    fastTicker=new Timer(25,this);
    void start(){
                     Listener ear = new Listener();
                     this.add(button);
                     button.setSize(100,50);
                     button.move(0,320);
                     button.addActionListener(ear);
                     this.add(button2);
                     button2.setSize(100,50);
                     button2.move(600,320);
                     button2.addActionListener(ear);
        fastTicker.start();
class Listener implements ActionListener{
  public void actionPerformed(ActionEvent b)
    Object o =b .getSource();
    if (o == button){
      if(dxshooter==3){
          dxshooter=-3;}
    if (o == button2){
    if(dxshooter==-3){
        dxshooter=3;}
   public void actionPerformed(ActionEvent e){     
   yposkogel=312-ykogel;
   ovalx1=xpos;
   ovalx2=xpos+100;
   ovalx3=xpos+200;
   ovalx4=xpos+300;
   ovalx5=xpos+400;
   ovalx6=xpos+500;
   ovalxb1=xpos+19;
   ovalxb2=xpos+19+100;
   ovalxb3=xpos+19+200;
   ovalxb4=xpos+19+300;
   ovalxb5=xpos+19+400;
   ovalxb6=xpos+19+500;   
              //bolletjesanimatie
               if(e.getSource()==fastTicker){
                ykogel+=dxkogel;
                xposkogel=xpos2;
                xpos2+=dxshooter;
                 if(ykogel>=3){
                 xposkogel=xposkogel;}          
                //schietding
                    if (xpos2>240){
                            dxshooter=-3;}
                    if(xpos2<-210){
                        dxshooter=3;}
               //kogelanimatie
                   if(ykogel>310){
                   ykogel=0;}
               //COMPARING THE POSITION OF THE BULLET WITH THE UFO's
            if(yposkogel<=15){
                    if(xposkogel>=ovalx6&&xposkogel<=ovalx6+60){
                    ovaly6=1500;
                    ovalyb6=1500;}
                    if(xposkogel>=ovalx5&&xposkogel<=ovalx5+60){
                    ovaly5=1500;
                    ovalyb5=1500;}
                    if(xposkogel>=ovalx4&&xposkogel<=ovalx4+60){
                    ovaly4=1500;
                    ovalyb4=1500;}
                    if(xposkogel>=ovalx3&&xposkogel<=ovalx3+60){
                    ovaly3=1500;
                    ovalyb3=1500;}
                    if(xposkogel>=ovalx2&&xposkogel<=ovalx2+60){
                    ovaly2=1500;
                    ovalyb2=1500;}
                    if(xposkogel>=ovalx1&&xposkogel<=ovalx1+60){
                    ovaly1=1500;
                    ovalyb1=1500;}
                 this.repaint();
public void paintComponent(Graphics g){
             super.paintComponent(g);
             //ufo's
             g.setColor(Color.black);
             g.fillOval(ovalxb1,ovalyb1, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx1,ovaly1,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb2,ovalyb2, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx2,ovaly2,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb3,ovalyb3, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx3,ovaly3,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb4,ovalyb4, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx4,ovaly4,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb5,ovalyb5, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx5,ovaly5,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb6,ovalyb6, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx6,ovaly6,width,height);
            //schietding
             g.fillRect(xpos2+350-17,317,6,40);
             g.setColor(Color.green);
             g.fillOval(xpos2+350-30,330,30,30);
             g.setColor(Color.black);
             g.fillRect(xpos2+350-40,350,50,height);
            //kogel
             g.fillRect(xposkogel+350-16,yposkogel,5,5);     
             public class AnimationDemo{
                 JFrame frame;
                 Animation animation;
                 void demo(){
                     frame=new JFrame("**Spees Inveeders** All rights reserved (Dave Senden and Joeri Oomen)");
                     animation=new Animation();
                     frame.getContentPane().add(animation);
                     frame.setSize(700,400);
                     frame.setVisible(true);
                     animation.start();
  public static void main(String[] args){
        AnimationDemo animationDemo=new AnimationDemo();
        animationDemo.demo();

thanks a lot! The buttons now work! I have updated my code, but now my problem is to compare the position of the bullet with the position of the ufo. I have marked the part where I wrote my solution, but it doesn't really work, do you have any suggestions?
Thanks.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Animation extends JPanel implements ActionListener{
    JButton button=new JButton("links");
    JButton button2=new JButton("rechts");
    int xpos=60;
    int xpos2=0;
    int height=15;
    int width=60;
    int dx=3;
    int dxshooter=3;
    int shoot=300;
    int ykogel=0;
    int dxkogel=6;
    int number=12;
    int xposkogel;
    int yposkogel;
    int ovalx1=xpos,ovalx2=xpos+100,ovalx3=xpos+200,ovalx4=xpos+300,ovalx5=xpos+400,ovalx6=xpos+500;
    int ovaly1=15,ovaly2=15,ovaly3=15,ovaly4=15,ovaly5=15,ovaly6=15;
    int ovalxb1=xpos+19,ovalxb2=xpos+19+100,ovalxb3=xpos+19+200,ovalxb4=xpos+19+300,ovalxb5=xpos+19+400,ovalxb6=xpos+19+500;
    int ovalyb1=5,ovalyb2=5,ovalyb3=5,ovalyb4=5,ovalyb5=5,ovalyb6=5;
    Timer fastTicker;
    Animation(){
    fastTicker=new Timer(25,this);
    void start(){
                     Listener ear = new Listener();
                     this.add(button);
                     button.setSize(100,50);
                     button.move(0,320);
                     button.addActionListener(ear);
                     this.add(button2);
                     button2.setSize(100,50);
                     button2.move(600,320);
                     button2.addActionListener(ear);
        fastTicker.start();
class Listener implements ActionListener{
  public void actionPerformed(ActionEvent b)
    Object o =b .getSource();
    if (o == button){
      if(dxshooter==3){
          dxshooter=-3;}
    if (o == button2){
    if(dxshooter==-3){
        dxshooter=3;}
   public void actionPerformed(ActionEvent e){     
   yposkogel=312-ykogel;
   ovalx1=xpos;
   ovalx2=xpos+100;
   ovalx3=xpos+200;
   ovalx4=xpos+300;
   ovalx5=xpos+400;
   ovalx6=xpos+500;
   ovalxb1=xpos+19;
   ovalxb2=xpos+19+100;
   ovalxb3=xpos+19+200;
   ovalxb4=xpos+19+300;
   ovalxb5=xpos+19+400;
   ovalxb6=xpos+19+500;   
              //bolletjesanimatie
               if(e.getSource()==fastTicker){
                ykogel+=dxkogel;
                xposkogel=xpos2;
                xpos2+=dxshooter;
                 if(ykogel>=3){
                 xposkogel=xposkogel;}          
                //schietding
                    if (xpos2>240){
                            dxshooter=-3;}
                    if(xpos2<-210){
                        dxshooter=3;}
               //kogelanimatie
                   if(ykogel>310){
                   ykogel=0;}
               //COMPARING THE POSITION OF THE BULLET WITH THE UFO's
            if(yposkogel<=15){
                    if(xposkogel>=ovalx6&&xposkogel<=ovalx6+60){
                    ovaly6=1500;
                    ovalyb6=1500;}
                    if(xposkogel>=ovalx5&&xposkogel<=ovalx5+60){
                    ovaly5=1500;
                    ovalyb5=1500;}
                    if(xposkogel>=ovalx4&&xposkogel<=ovalx4+60){
                    ovaly4=1500;
                    ovalyb4=1500;}
                    if(xposkogel>=ovalx3&&xposkogel<=ovalx3+60){
                    ovaly3=1500;
                    ovalyb3=1500;}
                    if(xposkogel>=ovalx2&&xposkogel<=ovalx2+60){
                    ovaly2=1500;
                    ovalyb2=1500;}
                    if(xposkogel>=ovalx1&&xposkogel<=ovalx1+60){
                    ovaly1=1500;
                    ovalyb1=1500;}
                 this.repaint();
public void paintComponent(Graphics g){
             super.paintComponent(g);
             //ufo's
             g.setColor(Color.black);
             g.fillOval(ovalxb1,ovalyb1, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx1,ovaly1,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb2,ovalyb2, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx2,ovaly2,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb3,ovalyb3, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx3,ovaly3,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb4,ovalyb4, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx4,ovaly4,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb5,ovalyb5, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx5,ovaly5,width,height);
             g.setColor(Color.black);
             g.fillOval(ovalxb6,ovalyb6, 25,25); 
             g.setColor(Color.orange); 
             g.fillOval(ovalx6,ovaly6,width,height);
            //schietding
             g.fillRect(xpos2+350-17,317,6,40);
             g.setColor(Color.green);
             g.fillOval(xpos2+350-30,330,30,30);
             g.setColor(Color.black);
             g.fillRect(xpos2+350-40,350,50,height);
            //kogel
             g.fillRect(xposkogel+350-16,yposkogel,5,5);     
             public class AnimationDemo{
                 JFrame frame;
                 Animation animation;
                 void demo(){
                     frame=new JFrame("**Spees Inveeders** All rights reserved (Dave Senden and Joeri Oomen)");
                     animation=new Animation();
                     frame.getContentPane().add(animation);
                     frame.setSize(700,400);
                     frame.setVisible(true);
                     animation.start();
  public static void main(String[] args){
        AnimationDemo animationDemo=new AnimationDemo();
        animationDemo.demo();

Similar Messages

  • How do i remove an Object (aliens/ bullets) from my Space Invaders game??

    Creating Space Invaders game and im getting confused on removing objects from my game. How do you actually remove an object (i.e bullets and aliens).
    I know this isnt the right way to do it, but for the bullets i had an array of the Objects bullets[] and set the x and y coordinates off screen so u cant see then wen they are painted you cant see them, then wen the player fires them, the x and y coordinates change so they come from the players ship till it hits an alien or goes off screen, then i reset the coordinates so they are pasted off screen again. Im relatively new to this so i would appreciate any help. Im going to have to do the same with aliens wen a collision is detected. Is there a way of actually removing an object?
    Here is sum code incase you need to see it:
    public void checkCollision()
              Rectangle playerRec = new Rectangle(player1.getBoundingBox());
              for(int i=0; i<alienRow; i++)
                   for(int j=0; j<alienCol; j++){
                        if(playerRec.intersects(aliens[i][j].getBoundingBox()))
                             collisionDetected();
                        for(int k=0; k<bulletNum; k++){
                             Rectangle bulletRec = new Rectangle(bullets[k].getBoundingBox());
                             if(bulletRec.intersects(aliens[i][j].getBoundingBox()))
                                  removeBullet(bullets[k]);
                                  collisionDetected();
         public static void collisionDetected()
              System.out.println("COLLISION");
    private void removeBullet(Bullets bullet){
              bullet.fired=false;
              bullet.setX(-10);
              bullet.setY(-10);
         }Edited by: deathwings on Nov 25, 2009 8:20 AM

    deathwings wrote:
    I was thinking bout that arraylist angle before, but it makes sense now so i think that will work ok. Thx kevin.Not a problem.
    Taking the List idea one step further, you could have a parent Object/Interface that all of your game objects extend/implement. Let's call it GameObject.
    A GameObject would have a draw(Graphics g) function that you call from your paintComponent( ) method, and for example an act( ) function as well.
    You'd have a single List of GameObjects that you add everything in your game into. Now painting is simply a matter of looping over a single List. And in your game logic loop, you could do a similar thing with the act( ) function: your bullet's act( ) would simply move it forward and check for collisions and going off screen. An alien's act( ) function would move it around and maybe shoot a weapon every so often.
    Just an idea.

  • Need Help with Java Space Invaders Game!

    Hi, im new to these forums so forgive if i dont follow the right way on laying things out. Im trying to create space invaders game but im stuck when im trying to create multiple aliens/ invaders. I have a class called Aliens (which is the invaders), and i can create an instance of an Alien. But when I try to create multiple instances they are not showing up on screen. I am not getting any error message though (which im hoping is a good sign, lol).
    GamePanel is where all the action is and Alien is the alien (invaders) class.
    Here is the code:
    import java.awt.; import javax.swing.; import java.awt.Image; import java.awt.event.KeyListener; import java.awt.event.KeyEvent;
    public class GamePanel extends JComponent implements KeyListener { SpaceInvaders game; static Player player1; Aliens alien1; static public Image spaceship2R; static public Image spaceship2L;
    public GamePanel(SpaceInvaders game)
         this.game = game;
         player1 = new Player(this);
         player1.start();
                   ///// trying to create multiple instances of aliens here
         for(int i=0; i<4; i++)
              alien1 = new Aliens(this);
              alien1.setX(i*100);
              alien1.start();
        this.setFocusable(true);
        this.addKeyListener(this);
    public void paintComponent(Graphics g)
         Image pic1 = player1.getImage();
         Image pic2 = alien1.getImage();
         super.paintComponent(g);
         g.drawImage(pic1, player1.getX(), player1.getY(), 50, 50,this);
         g.drawImage(pic2, alien1.getX(), alien1.getY(), 50, 50,this);
    }//end class
    import java.awt.Image; import java.awt.*;
    class Aliens extends Thread { //variables GamePanel parent; private boolean isRunning; private int xPos = 0, yPos = 0; Thread thread; static char direction = 'r'; Aliens alien; static public Image alien1; static public Image alien2;
    public Aliens(GamePanel parent)
         this.parent = parent;
         isRunning = true;
    public void run()
         while(isRunning)
              Toolkit kit = Toolkit.getDefaultToolkit();
              alien1 = kit.getImage("alien1.gif");
              alien2 = kit.getImage("alien2.gif");
              try
                      thread.sleep(100);
                 catch(InterruptedException e)
                      System.err.println(e);
                 updateAliens();
                 parent.repaint();
    public static Image getImage()
         Image alienImage = alien1;
        switch(direction){
             case('l'):
                  alienImage = alien1;
                  break;
             case('r'):
                  alienImage = alien1;
                  break;
        return alienImage;     
    public void updateAliens()
         if(direction=='r')
              xPos+=20;
              if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
                   yPos+=50;
                   xPos-=20;
                   direction='l';
         else
              xPos-=20;
              if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
                   yPos+=50;
                   xPos+=20;
                   direction='r';
    public void setDirection(char c){ direction = c; }
    public void setX(int x){ xPos = x; }
    public void setY(int y){ yPos = y; }
    public int getX(){ return xPos; }
    public int getY(){ return yPos; }
    }//end classEdited by: deathwings on Oct 19, 2009 9:47 AM
    Edited by: deathwings on Oct 19, 2009 9:53 AM

    Maybe the array I have created is not being used in the paint method, or Im working with 2 different objects as you put it? Sorry, Im just learning and I appreciate your time and help. Here is my GamePanel Class and my Aliens class:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.Image;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferStrategy;
    public class GamePanel extends JComponent implements KeyListener
         SpaceInvaders game;
         static Player player1;
         static Bullets bullet;
         //static public Image spaceship2R;
         //static public Image spaceship2L;
         private BufferStrategy strategy;
         static int alienNum =0;
         static Aliens[] aliens;
         public GamePanel(SpaceInvaders game)
              this.game = game;
              player1 = new Player(this);
              player1.start();
              Aliens[] aliens = new Aliens[10];
              for(int i=0; i<4; i++)
                   aliens[i] = new Aliens(this);
                   aliens.setX(i*100);
                   aliens[i].start();
                   alienNum++;
              initialiseGame();
              this.setFocusable(true);
    this.addKeyListener(this);
         public void paintComponent(Graphics g)
              Image pic1 = player1.getImage();
              Image pic2 = aliens[0].getImage();
              Image bulletPic = bullet.getImage();
              super.paintComponent(g);
              g.drawImage(pic1, player1.getX(), player1.getY(), 50, 50,this);
              for ( int i = 0; i < alienNum; i++ )
                   g.drawImage(pic1, aliens[0].getX(), aliens[0].getY(), 50, 50,this);
              //if (bullet.fired==true){
              //     g.drawImage(bulletPic, bullet.getX(), bullet.getY(), 20, 20,this);
         public void keyPressed(KeyEvent k)
              switch (k.getKeyCode()) {
              case (KeyEvent.VK_KP_DOWN):
              case (KeyEvent.VK_DOWN):
                   player1.setDirection('d');
                   break;
              case (KeyEvent.VK_KP_UP):
              case (KeyEvent.VK_UP):
                   player1.setDirection('u');
              break;
              case (KeyEvent.VK_KP_RIGHT):
              case (KeyEvent.VK_RIGHT):
                   player1.setDirection('r');
              break;
              case (KeyEvent.VK_KP_LEFT):
              case (KeyEvent.VK_LEFT):
                   player1.setDirection('l');
              break;
              case (KeyEvent.VK_SPACE):
                   fireBullet();
              break;
         public void keyTyped(KeyEvent k) {}//empty
         public void keyReleased(KeyEvent k)
              player1.setDirection('n');
         public void initialiseGame()
         public static void checkCollision()
              Rectangle playerRec = new Rectangle(player1.getBoundingBox());
              for(int i=0; i<alienNum; i++)
                   //if(playerRec.intersects(aliens[i].getBoundingBox()))
                        //collisionDetected();
         public static void collisionDetected()
              System.out.println("COLLISION");
         public void fireBullet()
              Bullets bullet = new Bullets(player1.getX(),player1.getY());
              bullet.fired=true;
    }//end GamePanelimport java.awt.Image;
    import java.awt.*;
    class Aliens extends Thread
         //variables
         GamePanel parent;
         private boolean isRunning;
         private int xPos = 0, yPos = 0;
         Thread thread;
         static char direction = 'r';
         Aliens alien;
         static public Image alien1;
         static public Image alien2;
         public Aliens(GamePanel parent)
              this.parent = parent;
              isRunning = true;
         public void run()
              while(isRunning)
                   Toolkit kit = Toolkit.getDefaultToolkit();
                   alien1 = kit.getImage("alien1.gif");
                   //alien2 = kit.getImage("alien2.gif");
                   try
              thread.sleep(100);
         catch(InterruptedException e)
              System.err.println(e);
              updateAliens();
              parent.repaint();
         public static Image getImage()
              Image alienImage = alien1;
         switch(direction){
              case('l'):
                   alienImage = alien1;
                   break;
              case('r'):
                   alienImage = alien1;
                   break;
         return alienImage;     
         public void updateAliens()
              if(direction=='r')
                   xPos+=20;
                   if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
                        yPos+=50;
                        xPos-=20;
                        direction='l';
              else
                   xPos-=20;
                   if(xPos >= SpaceInvaders.WIDTH-50||xPos < 0)
                        yPos+=50;
                        xPos+=20;
                        direction='r';
         public Rectangle getBoundingBox()
              return new Rectangle((int)xPos, (int)yPos, 50, 50);
         public void setDirection(char c){ direction = c; }
         public void setX(int x){ xPos = x; }
         public void setY(int y){ yPos = y; }
         public int getX(){ return xPos; }
         public int getY(){ return yPos; }
    }//end class                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Controlling fire in Space Invaders game

    Hi again,
    still working through the tutorial on Space Invaders in Flash
    MX 2004 (bit out of date, I know).
    I've got the player craft under mouse control. What I don't
    like about the game is that you can fire as rapidly as you wan't,
    though the number of bullets is limited. You can change this limit
    in the variable - I would rather have it though that you can only
    have a set number of bullets in play at a time (at the moment, if
    you go over the limit, the uppermost bullets just disapear).
    Here is the code:
    // when the mouse cliks
    onClipEvent(mouseDown)
    // duplicate the bullet mc
    _root.bullet.duplicateMovieClip("bullet"+bulletNum,bulletNum);
    // set the coords to the mouse clik
    eval("_root.bullet" + bulletNum)._x = this._x;
    eval("_root.bullet" + bulletNum)._y = this._y;
    // increment the bullet number
    bulletNum++;
    // if more than 50 bullets , start again at 0
    if(bulletNum>50)
    bulletNum = 0;
    What code could I use to have it so that you can only have,
    say, 3 bullets in play at once, and you can't fire again until
    they've hit / left the screen?
    regards,
    g

    Hi again,
    thanks for that. Another way would be to use code like the
    following:
    var enableInterval:Number;
    var allowFire:Boolean;
    function tempDisableFire():Void {
    allowFire = false;
    enableInterval = setInterval(enableFire, 500); // 500
    millisecond delay
    function enableFire():Void {
    allowFire = true;
    clearInterval(enableInterval);
    I am unsure though whether to insert this within my existing
    code, or to use this starting from scratch. If it was inside an
    onClipEvent tag (the event being the mouse click), I could then use
    it to control the fire. I'm not sure if I would do this with an "if
    / then" statement.
    Any suggestions on this method? I know it may be asking a bit
    much, but if you could even complete this piece of code that would
    be magic!
    cheers,
    g

  • Help with KeyDown (space invaders game)

    I'm making a space invaders type game for school using Greenfoot (new Java IDE). Whenever I press space, a bullet will fire. However, multiple bullets are being created when I press space once, and when I hold space, a long stream of bullets are being made. I want one bullet to be created when I press space. Tried using KeyPress, but apparently Greenfoot does not recognize java.awt? Thanks in advance.
    public class Ship extends Vehicle {
         * Constructor for objects of class Shuttle
        public Bullet B;
        boolean canFire = false;
        public Ship() {
            GreenfootImage ship = new GreenfootImage("spaceship.gif");
            this.setImage(ship);       
        //Create a bullet
        public void fire(){
            B = new Bullet();
            getWorld().addObject(B,getX(),getY()-60);
        public boolean collision(){
            Actor enemy = getOneIntersectingObject(null);
            return (enemy!=null) ? true : false;
        public void act(){
            // move with keys
            if (this.canMove())
                 move();      
            if(Greenfoot.isKeyDown("space")){               
                    fire();               
            else if(collision()){
                getWorld().removeObject(this);                           
    }

    Make a Class Variable to use as a flag indicating that a key was pressed... check to see if the flag is set, and if it is not, then fire a bullet and then set the flag. Reset the flag when they keyReleased action fires.
    This way only one bullet can be fired for each time a key is pressed, provided your act() is being polled, but if a keyPressed fires act(), but not a keyRelease too, then it will not work. If it does not work, then you'll need to go to a regular KeyListener.
    //Your code make these changes
    public class Ship extends Vehicle {
          * Constructor for objects of class Shuttle
         public Bullet B;
         boolean canFire = false;
         public Ship() {
             GreenfootImage ship = new GreenfootImage("spaceship.gif");
             this.setImage(ship);       
         //Create a bullet
         public void fire(){
             B = new Bullet();
             getWorld().addObject(B,getX(),getY()-60);
         public boolean collision(){
             Actor enemy = getOneIntersectingObject(null);
             return (enemy!=null) ? true : false;
         public void act(){
             // move with keys
             if (this.canMove())
                  move();      
            if(!GreenFoot.isKeyDown("space")) canFire = true; //add this
             if(Greenfoot.isKeyDown("space") and canFire){               
                     fire();              
                     canFire = false; //add this
             else if(collision()){
                 getWorld().removeObject(this);                           
    }

  • Firing bullets in space invaders game

    hey guys,
    I am trying to work out the logic and the code required for firing a bullet in a space invaders style game.
    I have a space ship on a stage that moves around freely using the arrow keys and I want to use the space bar to release a bullet.
    So far my thinking is as follows:
    when the space bar is pressed a bullet is released from the players space ship.
    so I need to think about:
    - the speed of the bullet
    - the position of the spaceship when the bullet is fired
    - being able to fire multiple bullets one after the other
    - the bullet should move from the bottom of the screen to the top of the screen
    - the consequence of a bullet hitting a target
    I do not know how many scripts I would need to tackle this? how many behaviours per script and more importantly the code required....
    I am desperate for any help and support you can provide...
    thank you

    thank you for your support
    at this current time it would be a waste of time to understand how things work that did not benefit my game, sure knowledge would give me power but at the moment Id rather concentrate on gaining the appropriate knowledge as per the mechanics of my game and not going off in the opposite direction
    having had a look at your space invaders game and the code and logic you provided it is different to the game I am building because:
    - you use the move for movement and fire whereas I use cursor keys and a space bar
    - you have limited the amount of ammuntion, I have not
    - there are no explosions or changing of cast members on intersection.
    I understand the aim of you posting the guide however I am of the thinking it would be better to follow a tutorial that is in line with what I require rather then learning a particular style and then again having to work out how to amend it again to be used in my game...
    So if anyone can help me understand or provide code as per my initial message , I would be so grateful
    thank you

  • Help! trying to create 'space invaders'

    hi. im a high school student with a little knowledge of java. im am currently working on a class assignment where each student has to create their own program. i decided to create the classic game of 'Space Invaders'. But i have a problem; i can't figure out how i will get the characters to move.
    does anyone if there is a method that will allow the user to move a character? for example, in C++ there is a conio.h library that has a goto(x,y) function. is there one for java as well? thanks alot!

    This is another way
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Si extends JFrame
         Timer  ta;
         myView view = new myView();
    public Si() 
         setBounds(3,10,765,350);
         addWindowListener(new WindowAdapter()
         {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);
         getContentPane().add("Center",view);
         setVisible(true);
         ta = new Timer(45, new ActionListener()
         {     public void actionPerformed(ActionEvent e)
                   view.move();
         ta.start();
    public class myView extends JComponent 
         Point invs[]   = new Point[15];
         Color invc[]   = new Color[15];
         Color  color[] = new Color[]{
                        Color.blue,   Color.cyan, Color.green, Color.magenta,
                    Color.orange, Color.pink, Color.red,   Color.yellow};
    public myView()
         for (int p=0; p < invs.length; p++)
              int x = (int)(Math.random()*600)+33;
              int y = (int)(Math.random()*200)+33;
              invs[p] = new Point(x,y);
              int c = (int)(Math.random()*8);
               invc[p] = color[c];
    public void move()
         for (int p=0; p < invs.length; p++)
              repaint(invs[p].x-1,invs[p].y-1,20,18);
              int x = (int)(Math.random()*3)-1;
              int y = (int)(Math.random()*3)-1;
              invs[p].translate(x,y);
              repaint(invs[p].x-1,invs[p].y-1,20,18);
    public void paintComponent(Graphics g)
         super.paintComponent(g);
         Graphics2D g2 = (Graphics2D)g;
         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);          
         for (int p=0; p < invs.length; p++)
              g.setColor(invc[p]);
              g.fillOval(invs[p].x,invs[p].y,17,13);
    public static void main (String[] args) 
         new Si();
    }      Noah

  • Space invaders! I need some help...!

    Hello,
    i'm trying to program a very simple version of space invaders. I'm drawing my moving objects (spaceship, rockets, aliens) with drawstring but I'm getting these compiling error: illegal start of expression.
    Also I want to move the spaceship with translate is this OK to do so!?
    Can somebody help me? Thanks for your help!
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Graphics2D;
    //in deze klasse teken ik het ruimteschip en kan het links/rechts bewegen
    public class Ruimteschip
         //ruimteschip
         Ruimteschip roderikschip = new Ruimteschip();
         public void paint(Graphics ruimteschip)
    {      //Ruimteschip weergeven
         Graphics2D ruimteschip2D = (Graphics2D)ruimteschip;
    String ruimteschip = "ruimteschip";
    ruimteschip2D.drawString("ruimteschip", x, y,);
         //beweegmethod
         public void beweegRechts()
         {     //teken opnieuw 1 coordinaat rechts
              roderikschip.translate(1, 0);
              ruimteschip2D.drawString("ruimteschip", x, y);
    //beweegmethod
         public void beweegLinks()
         {     //teken opnieuw 1 coordinaat links
              roderikschip.translate(-1, 0);
              ruimteschip2D.drawString("ruimteschip", x, y);
    }

    Hi and thanks for your replies.
    I know drawString isn't the most beautiful way, but I'm making this game for an assignment, and I have to use drawString.
    Still i'm getting "illegal start of expression" in my paint method...
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Graphics2D;
    //in deze klasse teken ik het ruimteschip en kan het links/rechts bewegen
    public class Ruimteschip
         //ruimteschip
            Ruimteschip ruimteschip = new Ruimteschip();
         public void paint(Graphics ruimteschip)
        {      //Ruimteschip weergeven
             Graphics2D ruimteschip2D = (Graphics2D)ruimteschip;
            String s = "ruimteschip";
            *ruimteschip2D.drawString(s, x, y,);*
         //beweegmethod
         public void beweegRechts()
         {     //teken opnieuw 1 coordinaat rechts
              s.translate(1, 0);
              ruimteschip.drawString(s, x, y);
        //beweegmethod
         public void beweegLinks()
         {     //teken opnieuw 1 coordinaat links
              s.translate(-1, 0);
              ruimteschip.drawString(s, x, y);
    } Edited by: Roddy on May 1, 2008 8:28 AM

  • Quadratic bezier curve: Y coordinate for a given X ?

    Hello,
    I have a quadratic bezier curve and I need the Y coordinate of a point on the bezier curve for a given X coordinate.
    I know that in pure maths this can be easily done, but I'm wondering if you really would have to do this manually in C# / WPF? Is it possible to get the single points used by C# / WPF for drawing the bezier curve and then maybe just loop them and compare the X coordinate of each point with the given X coordinate?(Well I guess the mathematical way would be more performant)
    For the mathematical way it would be good to know which step for the parameter t of the bezier curve has been choosen by C# / WPF? Any chance to find this out? Probably the steps for t are 1/(distance of P0 and P2) ? Thank you very much for any hint!

    hello jim,
    thanks for your reply. I've meanwhile solved my problem, I didn't understand the bezier formulas - they can be directly used to calculate a y coordinate for a given x (which I translated to t first).
    I however now have another problem:
    How to calculate the tangent of a given point of the quadratic bezier curve? I tried with the first derivation, however this gives me strange results (please compare [1]). Can someone please tell me, how to get the tangent of an arbitrary point on the quadratic bezier curve? Thank you very much!
    [1] http://cid-0432ee4cfe9c26a0.skydrive.live.com/self.aspx/%c3%96ffentlich/Quadratic%20Bezier%20Curve.pdf

  • How do I make a drop down list of text in numbers as it is made in the example spreadsheet for comparing cars for buying where you can choose a value from a dropdown list for each car?

    how do I make a drop down list of text in numbers as it is made in the example spreadsheet for comparing cars for buying where you can choose a value from a dropdown list for each car?

    Where is this example spreadsheet? Without seeing it I can only guess at what you are asking.
    To make a drop-down list (a pop-up menu in Numbers-speak), format the cell as a pop-up then edit and add to the list of items.
    If the example spreadsheet is pulling in a dollar value based on what car you chose in the pop-up, it is probably using LOOKUP or one of the other lookup functions, getting the information from another table (a lookup table). If, instead, these dollar values are what you are choosing in the pop-up, then you need to create a pop-up with these values in it.
    The Help menu includes a link to a page where you can download the Numbers Users Manual. It also has a link to the Formulas and Functions guide. Both are useful to new users.

  • Get location/coordinates for a field using JavaScript in Acrobat Professional

    Good day,
    I am looking for a way to get the coordinates of a text field.
    I would like to create a listbox and set the location of the listbox top left to the text fields bottom left coordinates, I have had a look at the scripting guides ... could not turn up anything.
    I know how to create and populate this listbox using the JavaScript code, only problem is, to get the coordinates of the text box.

    Try this:
    1. Move or change the dimensions of the text boxes on the extracted page in the configuration that you want.
    2. When you are satisfied with the new configuration, modify the following JavaScript to use the names of the text boxes that you want to include on the page.
    //find_rect //find coordinates of text boxes whose names are included in the var rct,ary = ["fld_1","fld_2","fld_3","fld_4"]; //...etc.
    for (j = 0; j<ary.length; j++){
    console.println(ary[j]);
    rct = this.getField(ary[j]).rect;
    for (i= 0; i< rct.length; i++) console.println(rct[i]);
    3. Add the modified code to the document level of form. (as a doc level javascript but with no function name or brackets included.
    4. When you run the code, the coordinates for the fields that you have included in the array will be printed in the console window:
    Example:
    fld_1
    21
    777
    296
    717
    fld_2
    30
    769
    65
    729

  • Can't activate the COMPARE function for object DNL_CUST_TVAST for Adapter

    Hello
    I've applier OSS note 866824 to enable the exchange of the sales area flag deletion. I had to do the initial download for the customizing object DNL_CUST_TVAST but it failed because the no intial load was permitted as the data distribution was active. So I've applied note 887696 to enable the intial load for this object : I've deleted the COMPARE function for this object with transaction R3AC3. I've done the initial load for this object and now I can't add the COMPARE function : the error message is enter linked bdoc type for synchronisation load. and I don't know what to do...
    if somebody can help me...
    thanks a lot

    Thanks for your tip but it does not work, I need to fill this field.
    I just have an answer from SAP support, this value COMPARE is relevant for loading data in CDB.
    there no sense to add the compare value for this object : the compare load can never be used for this object. It was a delivery error
    Edited by: Jerome Munier on Dec 5, 2008 7:13 PM

  • Check for spaces embedded with in a field

    Hi,
    I need to check for spaces in a field. The field is char(8). I need to check for spaces in between.
    Example:
    ' 1 2'
    '1 2'
    ' 12'
    Leading spaces and spaces between the chars. the spaces can be more than 1.
    Is this possible in just SQL?

    Hi !
    I have done "something" what shows you at which postion is space in string
    ( but i'm not very satisfied with this .. code :)
    SQL>
    SQL> with data as ( select ' 1 2' str from dual union all
      2                 select '1 2' str from dual union all
      3                 select ' 12' str from dual  )
      4  select distinct str,instr(str,' ',1,level) pos from
      5    (select str , max(level) i from data
      6      group by str
      7    connect by level <= length(str) - length(replace(str,' ','')) )
      8  connect by level <= i order by 1,2
      9  /
    STR         POS
    1 2          1
    1 2          3
    12           1
    1 2           2
    SQL> T

  • Why does Acrobat allocate more width for spaces than for alphanumeric characters?

    When I convert a text file to PDF using Acrobat X Pro, I select a fixed-width font.  However, it appears that Acrobat allocates a greater width for space characters in the text file, than it does for alphanumeric characters in the text file.  How do I instruct Acrobat to use exactly the same width for all the characters, both spaces and visible characters?
    This question is actually a re-write of my previous post titled, ‘Bulk Convert Text to PDF with Acrobat’.  I know what the problem is now but do not how to fix it.
    Here's a mock example of the content of a text file.  In the resulting PDF, the H no longer lies directly under the G.  I chose Courier New 8 pt for the font.
        ABCDEFG    ABCDEFG    ABCDEFG    ABCDEFG    ABCDEFG    ABCDEFG
              H          H          H          H          H          H
    Any suggestion will be appreciated.  Thank you.

    If you are comfortable with the settings in the printing, then go back to the PDF Maker settings (create PDF preferences in WORD) and adjust the preferences for similar performance, at least in the job settings and such. The rest deals with how much you want the bookmarks and such.
    I am assuming that those PDF Maker preferences set in WORD are retained when you run the conversion from Acrobat that I have the impression you are doing. Other than that, check the conversion settings in Acrobat itself. They are in the preferences. If you the printer preferences are used when converting from Text (you will have to try to see what happens), it would use the default preferences of the printer, not those set in the print menu of an application. The latter only last as long as that application is open and only for that application. For your need, you may need to change them in either the printer (Start>Printers.. in windows) or Distiller, or both. The printer overrides the Distiller settings usually (another setting on this).

  • What are the default settings for spaces

    What are the system's default settings for SPACES preferences?

    Below is a pict with Enable spaces checked so you can see the default popup menus.

Maybe you are looking for

  • Can anyone shed some light on this?

    We are having problems running two iPhones.  When a setting is changed on one one it changed the other.

  • Get date by group by per month basis...

    Hello all, I am trying to write a query where i want the count per month...so for example...the below query select to_char(date, 'MM/DD/YYYY') from test where rownum < 10; TO_CHAR(date 02/10/2009 02/10/2009 02/10/2009 02/10/2009 02/10/2009 05/31/2009

  • Making ADF Search case insensitive without using Execute with Params??

    I have a ADF search page baed on VO. I'm not Execute with Params form, instead i'm using normal ADF search page. Is it possible to make the search case insensitive?? if yes, how to do that?

  • Is there a log file for iDVD '08?

    I'm trying to burn a DVD using iDVD '08 and I can get the contents in iDVD '08, but when I try to burn the DVD, I get the popup message that says: "Encoding video: There was an error during movie encoding" and it quits. Is there a way (like a log fil

  • Regarding infotype 0002 enhancement

    hi Sirs,        I need to enhance infotype number '0002'in which i have to add terminal id field so that  it is easy to track the person who is changing the datas plz let me know how to that which user exit sud i use and how the terminal id sud be re