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

2020/2/11

Actor's Weapon Problem

Rafiqi_Fariz Rafiqi_Fariz

2020/2/11

#
i have weapon class, when it was touched by Robot class the robot hold the weapon. The weapon can also shoot by clicking the mouse. When Robot was lose its life i setLocation of the robot to the (48,50) coordinates. automatically Robot didn't held the weapon and weapon's location is set to where the Robot was died. The problem is, i still can shoot how is the solution for this case
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Robot here.
 * 
 * @author (Aulia El Ihza Fariz Rafiqi) sorry for my Englonesian (English and indonesian)
 * @version (1.7)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Weapon here.
 * 
 * @author (Aulia El Ihza Fariz Rafiqi) 
 * @version (1.7)
 */
public class Weapon extends SmoothMover
{
  
    Weapon(){
        
    }
    public void act() 
    {
        if(Greenfoot.mouseMoved(null)){
            MouseInfo mouse = Greenfoot.getMouseInfo();
            int rx = mouse.getX()-getX();
            int ry = mouse.getY()-getY();
            int angle = (int)(Math.atan2(ry,rx)*180.0/Math.PI);
            setRotation(angle);
        }
        
        if(Greenfoot.mouseClicked(null)){
            Greenfoot.playSound("ak47sound.wav");
            MouseInfo mouse = Greenfoot.getMouseInfo();
            System.out.println(mouse.getActor());
            int rx= mouse.getX()-getX();
            int ry= mouse.getY()-getY();
            double r = Math.sqrt(rx*rx+ry*ry);
            double angle = Math.atan2(ry,rx);
            int w=(int)(0.5*getImage().getWidth());
            int posx=(int)(getX()+w*Math.cos(angle));
            int posy=(int)(getY()+w*Math.sin(angle));
            Peluru peluru = new Peluru();
            peluru.setVeloc(50*rx/r,50*ry/r);
            getWorld().addObject(peluru,posx,posy);
        }else{
            Greenfoot.mouseClicked(this);
        }
    } 
}
public class Robot extends Actor
{
    /**
     * Act - do whatever the Robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Weapon senjata = new Weapon();
    private GreenfootImage myImage = getImage();
    // animasi robot orang berjalan
    private String[][] imageNames = {
                {"S_Right0.png","S_Right1.png","S_Right3.png",
                "S_Right4.png","S_Right5.png","S_Right6.png","S_Right7.png", "S_Right8.png"},
                {"S_Up0.png","S_Up1.png","S_Up3.png",
                 "S_Up4.png","S_Up5.png","S_Up6.png","S_Up7.png", "S_Up8.png"},
                {"S_Down0.png","S_Down1.png","S_Down3.png",
                 "S_Down4.png","S_Down5.png","S_Down6.png","S_Down7.png", "S_Down8.png"}
                };
    private int currentImage = 0;
    public int xPos;
    public int yPos;
    private int nyawa = 3;
    private int pizzaEaten = 0;
    private int weaponPicked = 0;
    private int timer;
    private int animationTimer = 0;
    private int walkspeed = 3;
    private final int MAXTIMER = 1000;
    
    public void addedToWorld(World w){
        xPos = getX();
        yPos = getY();
    }
    public void act() 
    {
        // Add your action code here.
        animationTimer++;
        if(animationTimer == 4){
            animateOnMove();
            animationTimer = 0;
        }
        robotMovement();
        eatPizza();
        detectWallCollision();
        detectBlockCollision();
        showStatus();
        updateTimer();
        detectHome();
        pegangSenjata();
    }
    
    Robot(){
        timer = MAXTIMER;
    }
    
    private void updateTimer(){
        timer--;
        getWorld().showText("Waktu: "+timer, 74, 490);
        if( timer == 0){
            removeLife();
            resetTimer();
        }
    }
    
    public void resetTimer(){
        timer = MAXTIMER;
    }
    public void robotMovement(){
        if(Greenfoot.isKeyDown("Right")){
            setLocation(getX()+walkspeed, getY());
        }else if(Greenfoot.isKeyDown("Left")){
            setLocation(getX()-walkspeed, getY());
        }else if(Greenfoot.isKeyDown("Up")){
            setLocation(getX(), getY()-walkspeed);
        }else if(Greenfoot.isKeyDown("Down")){
            setLocation(getX(), getY()+walkspeed);
        }else{
            setImage(imageNames[0][0]);
        }
    }
    
    public void removeLife(){
        resetPosition();
        nyawa--;
        testEndGame();
    }
    
    public void resetPosition(){
        setLocation(48,50);
    }
 
    public void detectWallCollision(){
        if(isTouching(Wall.class) || isTouching(BrickSmall.class) || isAtEdge()){
            Greenfoot.playSound("hurt.wav");
            removeLife();
            resetPizza();
            RobotWorld.setSkor();
        }
    }
    public void detectBlockCollision(){
        if(isTouching(Block.class)){
            Greenfoot.playSound("hurt.wav");
            removeLife();
            resetPizza();
            RobotWorld.setSkor();
        }
    }
    public void detectHome(){
        if(isTouching(Home.class)){
            Greenfoot.playSound("yipee.wav");
            ((RobotWorld)getWorld()).nextLevel();
            resetPosition();
            resetTimer();
        }
    }
    public void pegangSenjata(){
        
        //jika robot memegang senjata
        if(isTouching(Weapon.class)){
            weaponPicked++;
            if(weaponPicked == 1){
                Greenfoot.playSound("reload-pickup.wav");
            }
            removeTouching(Weapon.class);
            //ambil senjata
            getWorld().addObject(senjata, getX(), getY());
        }else{
            weaponPicked = 0;
        }
    }
}
danpost danpost

2020/2/11

#
In resetPosition method, before you reset robot's location, set the weapons location to (48, 50) if touching it. By the way, that will not be the weapon assigned to the senjata field (which should not be in your code).
Rafiqi_Fariz Rafiqi_Fariz

2020/2/13

#
i don't understand with this that will not be the weapon assigned to the senjata field (which should not be in your code).. can you give me an example?
Rafiqi_Fariz Rafiqi_Fariz

2020/2/13

#
if i set the location of the weapon to (48, 50) coordinates when it is touching. the Robot is always hold the weapon. i want the Robot release the weapon when it dies
Rafiqi_Fariz Rafiqi_Fariz

2020/2/13

#
i already finish this case by myself hhh. i make new method call fire and use it when it is touching by the robot. Thank you for your answers
You need to login to post a reply.