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

2017/11/10

WorldEdge cannot work help !

KauTC KauTC

2017/11/10

#
        public void Lattack()
   {
         if(Greenfoot.isKeyDown("J"))
        {
           Lanimateattack();
           Greenfoot.delay(4);
           World myWorld = getWorld();
           myWorld.addObject(Lattack, 0, 0);
           Lattack.setLocation(getX(), getY());
        }
   }

        public void Lanimateattack()
   {
        if(frame == 1)
        {
            setImage(LA);
            frame = 2;
        }
        else if(frame == 2)
        {
            Greenfoot.delay(5);
            setImage (LS);
            frame = 1;
        }
   }

       public boolean atWorldEdge()
   {
        if(getX() < 0 || getX() > getWorld().getWidth() - 100)
        return true;
        if(getY() < 0 || getY() > getWorld().getHeight() - 100)
        return true;
        else 
        return false;
   
   }

        public void RemoveatEdge()
   {
        if (atWorldEdge())  
        {  
            getWorld().removeObject(Lattack);  
        } 
   }        
why cant my world on edge work ?
danpost danpost

2017/11/10

#
KauTC wrote...
< Code Omitted > why cant my world on edge work ?
You will need to show the rest of the code in the class.
KauTC KauTC

2017/11/10

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Prince here.
 * 
 * @author Qwerty 
 * @version 1.0
 */
public class Prince extends Character
{
    private GreenfootImage LS = new GreenfootImage("LS.png");
    private GreenfootImage LM = new GreenfootImage("LM.png");
    private GreenfootImage RS = new GreenfootImage("RS.png");
    private GreenfootImage RM = new GreenfootImage("RM.png");
    private GreenfootImage RA = new GreenfootImage("RA.png");
    private GreenfootImage LA = new GreenfootImage("LA.png");
    private int frame = 1;
    private int animationCounter = 0;
    Lattack Lattack = new Lattack();
    Rattack Rattack = new Rattack();
    /**
     * Act - do whatever the Prince wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
   {
        move();
        animationCounter = animationCounter +1;
        Rattack();
        Lattack();
        RemoveatEdge();
   }
    
    public void move()
   {
        if(Greenfoot.isKeyDown("W"))
        {
            setLocation(getX(),getY()-4);
        }
        if(Greenfoot.isKeyDown("S"))
        {
            setLocation(getX(),getY()+4);   
        }
        if(Greenfoot.isKeyDown("A"))
        {
            setLocation(getX()-4,getY());
            if(animationCounter % 10 == 1)
            animateLeft();
        }
        if(Greenfoot.isKeyDown("D"))
        {
            setLocation(getX()+4,getY());
            if(animationCounter % 10 == 1)
            animateRight();
        }
   }
    
        public void animateLeft()
   {
        if(frame ==  1)
        {
            setImage(LM);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage (LS);
            frame = 1;
        }
   }
    
        public void animateRight()
   {
        if(frame ==  1)
        {
            setImage(RM);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage (RS);
            frame = 1;
        }
   }
    
        public void Rattack()
   {
        if(Greenfoot.isKeyDown("K"))
        {
           Ranimateattack();
           Greenfoot.delay(4);
           World myWorld = getWorld();
           myWorld.addObject(Rattack, 0, 0);
           Rattack.setLocation(getX(), getY());
        }
   }
    
        public void Lattack()
   {
         if(Greenfoot.isKeyDown("J"))
        {
           Lanimateattack();
           Greenfoot.delay(4);
           World myWorld = getWorld();
           myWorld.addObject(Lattack, 0, 0);
           Lattack.setLocation(getX(), getY());
        }
   }
    
        public void Ranimateattack()
   {
        if(frame == 1)
        {
            setImage(RA);
            frame = 2;
        }
        else if(frame == 2)
        {
            Greenfoot.delay(5);
            setImage (RS);
            frame = 1;
        }
   }
    
        public void Lanimateattack()
   {
        if(frame == 1)
        {
            setImage(LA);
            frame = 2;
        }
        else if(frame == 2)
        {
            Greenfoot.delay(5);
            setImage (LS);
            frame = 1;
        }
   }

        public boolean atWorldEdge()
   {
        if(getX() < 0 || getX() > getWorld().getWidth() - 100)
        return true;
        if(getY() < 0 || getY() > getWorld().getHeight() - 100)
        return true;
        else 
        return false;
   
   }
   
        public void RemoveatEdge()
   {
        if (atWorldEdge())  
        {  
            getWorld().removeObject(Lattack);  
        } 
   }        
}
danpost danpost

2017/11/10

#
Okay. I just wanted to make sure the 'RemoveatEdge' method (which calls the 'atWorldEdge' method) was being called by way of the 'act' method. Reason being you gave the impression that the 'atWorldEdge' method was not working at all. However, it appears that it works for the right and bottom edges of the world already. To fix the working for top and left edges, include zero in the condition on lines 142 and 144 (change '<' to '<='; or just to '==', if your world is not unbounded).
KauTC KauTC

2017/11/10

#
        public boolean atWorldEdge()
   {
        if(getX() <= 0 || getX() > getWorld().getWidth() - 100)
        return true;
        if(getY() <= 0 || getY() > getWorld().getHeight() - 100)
        return true;
        else 
        return false;
   
   }  
is it like that or ? anywhere i not sure why if my character is at the bottom when i haven even touch the world edge my wave wont even come out ?
danpost danpost

2017/11/10

#
If spawning your wave near the bottom, use 'getHeight()-2' (or less) for the y-coordinate of the spawning location.
KauTC KauTC

2017/11/10

#
Sorry i still not sure why the worldedge code not working ?!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Prince here.
 * 
 * @author Qwerty 
 * @version 1.0
 */
public class Prince extends Character
{
    private GreenfootImage LS = new GreenfootImage("LS.png");
    private GreenfootImage LM = new GreenfootImage("LM.png");
    private GreenfootImage RS = new GreenfootImage("RS.png");
    private GreenfootImage RM = new GreenfootImage("RM.png");
    private GreenfootImage RA = new GreenfootImage("RA.png");
    private GreenfootImage LA = new GreenfootImage("LA.png");
    private int frame = 1;
    private int animationCounter = 0;
    Lattack Lattack = new Lattack();
    Rattack Rattack = new Rattack();
    /**
     * Act - do whatever the Prince wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
   {
        move();
        animationCounter = animationCounter +1;
        Rattack();
        Lattack();
        RemoveAtEdge();
        atWorldEdge();
   }
    
    public void move()
   {
        if(Greenfoot.isKeyDown("W"))
        {
            setLocation(getX(),getY()-4);
        }
        if(Greenfoot.isKeyDown("S"))
        {
            setLocation(getX(),getY()+4);   
        }
        if(Greenfoot.isKeyDown("A"))
        {
            setLocation(getX()-4,getY());
            if(animationCounter % 10 == 1)
            animateLeft();
        }
        if(Greenfoot.isKeyDown("D"))
        {
            setLocation(getX()+4,getY());
            if(animationCounter % 10 == 1)
            animateRight();
        }
   }
    
        public void animateLeft()
   {
        if(frame ==  1)
        {
            setImage(LM);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage (LS);
            frame = 1;
        }
   }
    
        public void animateRight()
   {
        if(frame ==  1)
        {
            setImage(RM);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage (RS);
            frame = 1;
        }
   }
    
        public void Rattack()
   {
        if(Greenfoot.isKeyDown("K"))
        {
           Ranimateattack();
           Greenfoot.delay(4);
           World myWorld = getWorld();
           myWorld.addObject(Rattack, 0, 0);
           Rattack.setLocation(getX(), getY());
        }
   }
    
        public void Lattack()
   {
         if(Greenfoot.isKeyDown("J"))
        {
           Lanimateattack();
           Greenfoot.delay(4);
           World myWorld = getWorld();
           myWorld.addObject(Lattack, 0, 0);
           Lattack.setLocation(getX(), getY());
        }
   }
    
        public void Ranimateattack()
   {
        if(frame == 1)
        {
            setImage(RA);
            frame = 2;
        }
        else if(frame == 2)
        {
            Greenfoot.delay(5);
            setImage (RS);
            frame = 1;
        }
   }
    
        public void Lanimateattack()
   {
        if(frame == 1)
        {
            setImage(LA);
            frame = 2;
        }
        else if(frame == 2)
        {
            Greenfoot.delay(5);
            setImage (LS);
            frame = 1;
        }
   }

        public boolean atWorldEdge()
   {
        if(getX() <= 10 || getX() > getWorld().getWidth() - 100)
        return true;
        if(getY() <= 10 || getY() > getWorld().getHeight() - 2)
        return true;
        else 
        return false;
   
   }     
        
        public void RemoveAtEdge()
   { 
        if (atWorldEdge())  
        {  
            getWorld().removeObject(Lattack);
            getWorld().removeObject(Rattack);
        }
   }
}
Super_Hippo Super_Hippo

2017/11/10

#
Maybe you could describe what you want to do and what happens instead. At least I don't really get it... What are L/R attack and why do move them to the Prince when attacking and remove them when the Prince hits the edge of the world? Why is the 100 there?
danpost danpost

2017/11/11

#
danpost wrote...
If spawning your wave near the bottom, use 'getHeight()-2' (or less) for the y-coordinate of the spawning location.
This was meant in reference to your preparing of the world -- where you add the new actors into the world.
You need to login to post a reply.