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

2016/10/27

ShootingBall

danieli45 danieli45

2016/10/27

#
 xPosition = xPosition + xspeed;
        yPosition = yPosition + yspeed;
       
        MyWorld w = (MyWorld) getWorld();
        
         if (getOneIntersectingObject(brick.class) != null)
        {
        setImage(new GreenfootImage("brick.png"));
           setLocation(this.getX(), this.getY());
           w.allowBallLaunch();
          return;
        }
        
        if (getY() == getWorld().getHeight()-1)
            {
             w.allowBallLaunch();
             this.setLocation(this.getX(), this.getY());
             return;
            }
       
         if (xPosition >= getWorld().getWidth()-1)
        {
            getWorld().removeObject(this);
            return;
        }
        
         if  (yPosition <= 0 || yPosition >= getWorld().getHeight()-1)
        { 
            this.setImage(this.buildUpArrowImage());
            w.allowBallLaunch();
           
        }
   
        if (yPosition <= 0 || yPosition >= getWorld().getHeight()-1)
        {
            w.allowBallLaunch();
            return;
        }
        
        if (getOneIntersectingObject(StickyBall.class)!=null)
        {
           setLocation(this.getX(), this.getY());
          w.allowBallLaunch();
          return;
        }
this is the code I have but I only want to shoot a ball if the ball is not moving, it changed or is not in the world otherwise I cant shot another ball,
danpost danpost

2016/10/28

#
I am finding it difficult to follow your code here. I presume that the code is in a ShootingBall class, which creates ShootingBall objects. It does not matter what image you give the objects, they will always be ShootingBall objects. If you want to turn one into a brick, add a new brick object and remove the ShootingBall object ('this'). Possibly you should have an Arrow class also to do similarly with, instead of calling 'buildUpArrowImage'. With these changes, in all cases, no ShootingBall object will be in the world; so, your world class can just ask one question to shoot a new ball:
if (getObjects(ShootingBall.class).isEmpty())
By the way, you can remove all occurrences of the following line:
setLocation(this.getX(), this.getY());
// or
this.setLocation(this.getX(), this.getY());
It says to set the location of the actor to the location it is currently at; so, it is meaningless as it never produces any change in location. I also do not see how the 'xPosition' and 'yPosition' fields figure into the code. They do not appear to change with any change in the position of the actors created from the class. In fact, I do not see any code that actually cause the actors of the class to move or relocate at all.
You need to login to post a reply.