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

2015/4/28

Randomly enemy from top [HELP] Please

pandu4431 pandu4431

2015/4/28

#
i try to built a game with enemy have randomly movements, but even i try some code from this forum i just saw my enemy turn from top-left to right side. this my world code, what i should to do, please help master <sorry for bad english>
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot) 

/**
 * A class that has a scrolling background image
*/
public class ScrollWorld extends World 
{ 
    private static final GreenfootImage bgImage = new GreenfootImage("Background 2.jpg");
    private static final int scrollSpeed = 2;
 
    private GreenfootImage scrollingImage;
    private int scrollPosition = 0;
     private int spawnCounter = 0;
    public ScrollWorld()
    {
        super(400, 600, 1);
      
        GreenfootImage background = new GreenfootImage(400, 600);
        scrollingImage = getScrollingImage(400, 600);
        background.drawImage(scrollingImage, 0, 0);
        setBackground(background);
       
        addObject(new SpaceShip(), 200, 540);
        addObject(new Counter(),20,570);
        
    }

    
    public void act()
    {
        //if(scrollSpeed > 0 && scrollPosition <= 0) {
          //  scrollPosition = 0;
        //}
        if(scrollSpeed > 0 && scrollPosition >= getHeight()) {
            scrollPosition = 0;
        }
        //if (Greenfoot.getRandomNumber(100) <3)  {  
          //  addObject(new Asteroid(), 3 - 1, Greenfoot.getRandomNumber(getHeight()));  
        //}
        if (spawnCounter > 100) {
            spawnCounter = 0;
            addObject(new Asteroid(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        }
        spawnCounter++;
        scrollPosition += scrollSpeed;
        paint(scrollPosition);
    }
  
    /**
     * Paint scrolling image at given position and make sure the rest of
     * the background is also painted with the same image.
     */
    private void paint(int position)
    {
        GreenfootImage bg = getBackground();
        bg.drawImage(scrollingImage,0, position);
        bg.drawImage(scrollingImage,0, position - scrollingImage.getHeight());
    }
 
    private GreenfootImage getScrollingImage(int width, int height)
    {
        GreenfootImage image = new GreenfootImage(width, height);
        //for(int x = 0; x < width; x += bgImage.getWidth()) {
            for(int y = 0; y < height; y += bgImage.getHeight()) {
                image.drawImage(bgImage, x, y);
            }
        //}
        return image;
    } 
}
azazeel azazeel

2015/4/28

#
where is the enemy class ?
pandu4431 pandu4431

2015/4/28

#
in asteroid
danpost danpost

2015/4/28

#
azazeel wrote...
where is the enemy class ?
He means that you need to show the code for the class of the enemy in question.
pandu4431 pandu4431

2015/4/29

#
import greenfoot.*; /** * Write a description of class Aster here. * * @author (your name) * @version (a version number or a date)a */ public class Asteroid extends Mover { public boolean AtWorldEdge() { double w=getImage().getWidth(),h=getImage().getHeight(); return getX()==w || getY()==h || getX()==getWorld().getWidth()-(w+1) || getY()==getWorld().getHeight()-(h+1); } /** * Act - do whatever the rock wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX(), getY()+5); int r = Greenfoot.getRandomNumber ( 50 ); setRotation (getRotation()+r); if (AtWorldEdge()) { int ran = Greenfoot.getRandomNumber ( 500 ); getWorld().addObject( new Asteroid(), ran, 0); getWorld().removeObject(this); } } }
danpost danpost

2015/4/29

#
When your asteroids hit a world edge, you are placing new ones along the top edge of the world, which are still hitting an edge (even if you do move them down 5 pixels before checking for hitting an edge, unless your asteroids are less than 5 pixels in height).
pandu4431 pandu4431

2015/4/29

#
emmh, can you give me code to make my asteroid comes from top and move into the bottom page? and tell me, where i must copy that code, please help me, i'm noob
danpost danpost

2015/4/29

#
Since you asteroids move straight down, there is really only one edge that they are concerned with. Change your 'AtWorldEdge' method to an 'AtBottomEdge' method. Then, you will not have problems at the top edge.
pandu4431 pandu4431

2015/4/29

#
oh, yeah. thanks master,,,, thanks a lot for this tutorial
You need to login to post a reply.