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

2015/2/27

Help spawning into world of selected stages

1
2
RagingAsian RagingAsian

2015/3/3

#
I changed the value of Distance to 1000.
RagingAsian RagingAsian

2015/3/3

#
never mind I got the less than symbol and greater than symbol mixed up, but towers won't turn towards creeps correctly.
danpost danpost

2015/3/3

#
fejfo wrote...
and if the new code is in creeper Creepers still spawn it self I would make a Creeper spawner with makes creepers (the creeper spawner can be an inviseble object)
You do not need an invisible object to spawn objects. You already have a world object whose act method can do that for you.
danpost danpost

2015/3/3

#
Sorry, what I had here seems to have been resolved already.
RagingAsian RagingAsian

2015/3/3

#
Right now, Im trying to get my creeps to spawn into the world but still having no success.
RagingAsian RagingAsian

2015/3/3

#
Here is what I currently have: World Stage1 Code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Stage1 here. 
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Stage1 extends World
{
    int[]WayX;
    int[]WayY;
    int index=0;
    public static int hp;
    public static int speed;
    private int counter=0;;
    private static final int max=20;
    private int spawn=0;
    private static int level = 1;
    /**
     * Constructor for objects of class Stage1.
     * 
     */
    public Stage1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 425, 2); 
        setBackground("Stage1f.png");
        prepare();
    }
    
    public int getLevel()
    {
        return level;
    }
    
    public void act(int hp,int speed)
    {
        counter++;
        
        if((counter>=20)&&(spawn<=max))
        {
            System.out.println("Test");
            addObject(new Creepers(hp,speed,level),0,108);            
            spawn();
            spawn++;
            waypoints();
            counter=0;
        }
        
        
    }
    
    public void spawn()
    {            
        int random= (int)((Math.random()*3) + 1);
       
        if(random==1)
        {            
            addObject(new Small(level),0,108);           
        }
        if(random==2)
        {
            addObject(new Medium(level),0,108);
        }
        if(random==3)
        {
            addObject(new Heavy(level),0,108);
        }
        System.out.println(random);
    }
    
    public void waypoints()
    {
         WayX= new int[]{94,94,188,188,281,281,374,374,467,467,563,563,599};
         WayY= new int[]{108,223,223,28,28,372,372,83,83,314,314,140,140};
         index++;
    }
   
    public void prepare()
    {
        Counter counter = new Counter();
        addObject(counter, 123, 416);
        counter.setLocation(120, 415);
        Counter2 counter2 = new Counter2();
        addObject(counter2, 240, 415);
        counter2.setLocation(237, 414);

        NormalButton nbutton = new NormalButton(counter2);
        addObject(nbutton, 15, 416);
        nbutton.setLocation(12, 414);

        IceButton ibutton = new IceButton(counter2);
        addObject(ibutton, 26, 409);
        ibutton.setLocation(49, 413);
        ibutton.setLocation(51, 413);
        ibutton.setLocation(48, 413);

        FireButton fbutton = new FireButton(counter2);
        addObject(fbutton, 31, 415);
        fbutton.setLocation(30, 414);
        fbutton.setLocation(31, 414);
        fbutton.setLocation(31, 414);

        SniperButton sbutton = new SniperButton(counter2);
        addObject(sbutton, 70, 415);
        sbutton.setLocation(66, 414);
        sbutton.setLocation(69, 414);

        BombButton bbutton = new BombButton(counter2);
        addObject(bbutton, 89, 417);
        bbutton.setLocation(85, 414);

        Money money = new Money();
        addObject(money, 195, 409);
        money.setLocation(193, 414);

        Lives lives = new Lives();
        addObject(lives, 424, 395);
        lives.setLocation(112, 415);       

        counter.setLocation(145, 414);
        counter.setLocation(149, 414);
        lives.setLocation(117, 414);
        counter.setLocation(151, 414);
        counter2.setLocation(237, 413);
    }
}
Creepers SuperClass Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Creepers here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Creepers extends Actor
{   
    public static int type;
    int[]WayX;
    int[]WayY;
    int index=0;
    double distance=150;
    int count=0;
    private int max=20;
    private int spawn=0;
    public static int hp;
    public static int speed;
    static Counter counter;
    private int level;
    
    public Creepers(int hp,int speed, int lv)
    {
        level = lv;
        hp=hp;
        speed=speed;
        setRotation(90); 
        if(type==1) setImage("Enemy-small.png");
        if(type==2) setImage("Enemy-medium.png");
        if(type==3) setImage("Enemy-heavy.png");
        if (IntroScreen.level==3)
        {
            setRotation(180);
        }
        
        
        
    }
    
    public void Act() 
    {
        Move();
//         Waypoints();

    }
        
       
     
    
//     public void Waypoints()
//     {
// 
//         if(Stage1.getLevel()== 1)
//         {
//            
//             
//            WayX= new int[]{94,94,188,188,281,281,374,374,467,467,563,563,599};
//            WayY= new int[]{108,223,223,28,28,372,372,83,83,314,314,140,140};
//            Move();
//         }
//         if (IntroScreen.level == 2){
//            WayX= new int[] {478,487,272,272,599};
//            WayY= new int[]{171,380,380,19,19};
//         }
//         if (IntroScreen.level == 3){
//            WayX=new int[] {355,246,246,463,463,134,134,572,572,27,27};
//            WayY=new int[] {248,248,115,115,314,314,49,49,381,381,10};
//         }
//         if (IntroScreen.level == 4){
//            WayX= new int[]{319,319,56,56,431,431,525,525,599};
//            WayY= new int[]{376,21,21,288,288,21,21,375,375};
//         }
//     }
    
    public void Move()
    {
        move(1);        
        double theDistance = (int)(Math.hypot(WayX[index] - getX(), WayY[index] - getY()));   
        if (theDistance < 1  )
        {
            
             for (int i=0;i<theDistance;i++)
             {
                 distance=theDistance;
             }
             
                  
        } 
        turnTowards(WayX[index], WayY[index]);
        if(count>=598&&spawn>=598)
        {
            getWorld().removeObject(this);
        }
    }
    
    public void Death()
    {
        World world = getWorld();
        Actor bullet = getOneIntersectingObject(Bullets.class);
//         if (getIntersectingObjects(Bullets.class)!=null)
//         {
//             getWorld().removeObject(this);
//         }
    }
    
 }
danpost danpost

2015/3/3

#
By convention, method names should begin with lowercase letters. And this may be the cause of your problems, as the names of the World and Actor class methods are 'act', not 'Act'.
RagingAsian RagingAsian

2015/3/3

#
I changed all first letters in method names to lower case, but still need help spawning creeps into world.
RagingAsian RagingAsian

2015/3/3

#
nevermind fix it with my teacher.
RagingAsian RagingAsian

2015/3/4

#
I have another problem. I set values for objects in their constructors, how would I set those values to be used? For example, in the Creepers Class, i set a speed variable, how would I use the speed variable from the contractor to be the actual speed of the creeper? I made some attempts but when I compiled, all creepers moved at the same speed, when they all have different speed values?(Sorry if my question sounds confusing, not very good at explaining)
danpost danpost

2015/3/4

#
You have several fields in your Crreeper class that are declared 'static'. 'hp', 'speed' and 'type' are all supposed to be unique values for each creeper which would require those fields to be 'non-static'. Remove 'static' from those lines.
RagingAsian RagingAsian

2015/3/4

#
how would i use the values that are set in the constructors?
danpost danpost

2015/3/5

#
Well, you are shadowing the instance fields by using local variables in the constructor with the same exact names as the fields ("hp" and "speed"). To save the value of those variables in the instance fields, you need to use the following:
this.hp = hp;
this.speed = speed;
where 'this.hp' and 'this.speed' are the instance fields (declared on lines 19 and 20) and 'hp' and 'speed' are the variables (declared in the constructor declaration statement on line 24).
RagingAsian RagingAsian

2015/3/5

#
Should they stay private or change them to public? I posted the scenario as well on my profile.
You need to login to post a reply.
1
2