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

2016/4/18

I want my actor to hold a gun

Amadeus Amadeus

2016/4/18

#
I want my actor to hold a gun and so i wrote this code.
public void stickingToThePlayer(){
        Actor player = getOneObjectAtOffset(0, 0, Players.class);
        Actor player2 = getOneObjectAtOffset(-40, +10, Players.class);
        Actor player3 = getOneObjectAtOffset(40, +10, Players.class);
        Actor player4 = getOneObjectAtOffset(0, -40, Player.class);
        if(Greenfoot.isKeyDown("right")&&!Greenfoot.isKeyDown("left")&&!Greenfoot.isKeyDown("up")){
            if(player != null){
                setLocation(player.getX()+40, player.getY()+10);
            }
            if(player3 != null){
                setLocation(player.getX()+40, player.getY()+10);
            }
        }
        if(Greenfoot.isKeyDown("left")&&!Greenfoot.isKeyDown("right")&&!Greenfoot.isKeyDown("up")){
            if(player != null){
                setLocation(player.getX()-40, player.getY()+10);
            }
            if(player2 != null){
                setLocation(player.getX()-40, player.getY()+10);
            }
        }
        if(Greenfoot.isKeyDown("up")){
            if(player != null){
                setLocation(player.getX()+40, player.getY()+10); 
            } 
            if(player2 != null){
                setLocation(player.getX()+40, player.getY()+10);
            }
            if(player4 != null){
                setLocation(player.getX()+40, player.getY()+10);
            }
        }
    }
And now i have 2 problems. The first one is that i always get this error once i change my direction of movement (from left to right, or from right to up and etc.)
java.lang.NullPointerException
	at rayGun.stickingToThePlayer(rayGun.java:52)
	at rayGun.act(rayGun.java:17)
	at greenfoot.core.Simulation.actActor(Simulation.java:594)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:552)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
The other problem is when i fall, the gun isnt following.
danpost danpost

2016/4/18

#
The location of the gun should be controlled in the class of the player -- not in the gun class (guns do not move on their own).
Amadeus Amadeus

2016/4/18

#
Thank you for your reply, but my problem would be then, that i do not know how to change the position of another Class with my actor.
Amadeus Amadeus

2016/4/18

#
Ok I managed to solve the Problem by changing my code to this.
public void stickingToThePlayer(){
        /*Actor player = getOneObjectAtOffset(0, 0, Players.class);
        Actor player2 = getOneObjectAtOffset(-40, +10, Players.class);
        Actor player3 = getOneObjectAtOffset(40, +10, Players.class);
        Actor player4 = getOneObjectAtOffset(0, -40, Players.class);*/
        Actor player = (Actor)(getWorld().getObjects(Players.class).get(0));
        
        if(Greenfoot.isKeyDown("right")&&!Greenfoot.isKeyDown("left")&&!Greenfoot.isKeyDown("up")){
            if(player != null){
                setLocation(player.getX()+40, player.getY()+10);
            }
            /*if(player3 != null){
                setLocation(player3.getX()+40, player.getY()+10);
            }*/
        }
        if(Greenfoot.isKeyDown("left")&&!Greenfoot.isKeyDown("right")&&!Greenfoot.isKeyDown("up")){
            if(player != null){
                setLocation(player.getX()-40, player.getY()+10);
            }
            /*if(player2 != null){
                setLocation(player2.getX()-40, player.getY()+10);
            }*/
        }
        if(Greenfoot.isKeyDown("up")){
            if(player != null){
                setLocation(player.getX()+40, player.getY()+10); 
            } 
            /*if(player2 != null){
                setLocation(player2.getX()+40, player.getY()+10);
            }
            if(player4 != null){
                setLocation(player4.getX()+40, player.getY()+10);
            }*/
        }
        if(player.getY()+10-getY()>=11){
            setLocation(player.getX()+40, player.getY()+10);
        }
    }
danpost danpost

2016/4/18

#
It would be best to have the player create and control the gun:
// instance field (outside methods but inside player class)
private Gun gun = null;

// when picked up or given to player
gun = new Gun(); // or 'gun = <? some reference to a Gun object ?>'

// when removing from holster (after checking that 'gun != null && gun.getWorld() == null)
getWorld().addObject(gun, getX(), getY()+10);
int facing = <? facing left ?> ? -1 : 1;
gun.move(facing*40);

// after moving when not holstered (after checking that 'gun != null && gun.getWorld() != null')
gun.setLocation(getX(), getY()+10);
int facing = <? facing left ?> ? -1 : 1;
gun.move(facing*40);

// when holstering or dropping gun (after checking 'gun != null && gun.getWorld() != null')
getWorld().removeObject(gun);

// when player no longer has the gun (after checking 'gun != null)
if (gun.getWorld() != null) getWorld().removeObject(gun); // optional
gun = null;
If game can continue after player is removed from the world, make sure any gun the player has is also removed.
Amadeus Amadeus

2016/4/19

#
Yeah that indeed looks like a better way to do it. Gonna put it in my code thank you, the only thing why i didnt do it this way from the start on is, that i am new to programming and this is my first project, so i do not know so many possibilitys yet.
danpost danpost

2016/4/19

#
Amadeus wrote...
Yeah that indeed looks like a better way to do it. Gonna put it in my code thank you, the only thing why i didnt do it this way from the start on is, that i am new to programming and this is my first project, so i do not know so many possibilitys yet.
Yeah. Well, it is always best to have whatever action an object type does should go in its class. That is, for example, a player holds a gun and carries it around; so, that part should be programmed in the class of the player. A gun fires a bullet; so that part should be programmed in the class of the gun. The gun could also, if you wanted to be really detailed in your work, recoil and push the player back slightly; or, chamber another bullet (as opposed to the player doing something to chamber the next round; it could have a limited supply of rounds or unlimited; it could let off a puff of smoke, if you wanted; etc. So, with programming in general, if you state something in the form of '<noun><verb>' (player holds gun), code the action (<verb>) in the class of the subject (<noun>). If the verb has the meaning of 'has' (such as 'player has health') or 'owns' (such as 'player has gun') then add an instance field to hold the quantity of the state or the reference to the object of possession. The verb 'gets' could also be used in the same context (player gets healthbar). If the verb is an action verb then you need a method to describe the action to perform (player moves; or player jumps; or player fires gun ).
You need to login to post a reply.