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

2013/11/15

beginner help

1
2
csr1010 csr1010

2013/11/15

#
I have a Zombie game I have been working on (cliche), and I have my person being able to shoot and also turn when different keys are pressed.(wasd) The character only shoots to the left though, How would I get it to shoot the direction the character is facing?
danpost danpost

2013/11/15

#
That would depend on what code you are using to set the facing direction of your character. Please show its class code.
csr1010 csr1010

2013/11/15

#
Here is the code for the bullet
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int adjust;
    public boolean exsists = true;
    public static int kills;
    private int gunCoolDown;
    public void act() 
    {
        move();
        checkForArrowKeys(adjust);
        if (canSee (Zombie.class)) 
        {
            kill(Zombie.class);
            exsists = false;
        }
        if (exsists)
        {
            checkEdges();
        }
    }
    public void kill(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
            getWorld().removeObject(this);
        }
    }
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0,0, clss);
        return actor != null;
    }
    public void move()
    {
        setLocation(getX() - 10, getY() );
    }
    public void checkEdges()
    {
        if (atWorldEdge())
        {
            getWorld().removeObject(this);
        }
    }
    public boolean atWorldEdge()  
    {  
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)  
            return true;  
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)  
            return true;  
        else  
            return false;  
    }  
    public void checkForArrowKeys(int adjust)
    {
            if ( Greenfoot.isKeyDown("a") )
        {
            setRotation(0);
            setLocation(getX() - adjust, getY() );
            
        }  
        if ( Greenfoot.isKeyDown("d") )
        {
            setRotation(180);
            setLocation(getX() + adjust, getY() );
            
        }
            if ( Greenfoot.isKeyDown("w") )
        {
            setRotation(90);
            setLocation(getX(), getY() -adjust );
            
        }
            if ( Greenfoot.isKeyDown("s") )
        {
             setRotation(270);
            setLocation(getX() , getY() + adjust );
             
        }   
    }
}   
    
and Here is the code for the character
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class Survivor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Survivor extends Actor
{
boolean ifKeyIsStillDown = false;
public static int score = 0;
//int shottimer = 0;
/**
* Act - do whatever the Survivor wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act() 
{
    checkForArrowKeys(3);

   //shottimer--;
}
public void checkForArrowKeys( int adjust)
{
    if ( Greenfoot.isKeyDown("a") )
    {
         setLocation(getX() - adjust, getY() );
         setRotation(0);
    }  
    if ( Greenfoot.isKeyDown("d") )
    {
         setLocation(getX() + adjust, getY() );
         setRotation(180);
    }
         if ( Greenfoot.isKeyDown("w") )
    {
         setLocation(getX(), getY() -adjust );
         setRotation(90);
    }
         if ( Greenfoot.isKeyDown("s") )
    {
         setLocation(getX() , getY() + adjust );
         setRotation(270);
    }
    if (! Greenfoot.isKeyDown("space") )
    {
        ifKeyIsStillDown = false;
    }
    if ( Greenfoot.isKeyDown("space") )
    {
         shoot();
         ifKeyIsStillDown = true ;
    }
}   
    public void shoot()  
{  
     
    {  
        if (!ifKeyIsStillDown)
        //if (shottimer < 1)
        {
        int rot = getRotation()-10;  
        Bullet b = new Bullet();  
        getWorld().addObject(b, getX(), getY());  
        
        //shottimer = 10;
        }
    }  
}  
}
danpost danpost

2013/11/15

#
The reason the code was necessary was because you used the word 'facing', and, although you did use the word 'turn', it was not an absolute that you were rotating the actor (would could have been changing the image itself to "turn" it). In Survivor class In line 64, you are declaring a local field 'rot' that you are not using (remove that line). Between lines 65 and 66, insert the following line:
b.setRotation(getRotation());
In Bullet class Remove line 22 and line 66 through 92 (the complete 'checkForArrowKeys' method). After doing the above, if you still have problems, we can resolve them from there.
csr1010 csr1010

2013/11/22

#
Thanks Dan, I have changed my code to make it so the guy always looks at the mouse. I am trying to figure out how to make him shoot towards the mouse when the mouse clicks. Here is my whole survivor code. public class Survivor extends Actor { boolean ifKeyIsStillDown = false; public static int score = 0; public int direction = 0; //int shottimer = 0; int mX; int mY; public void act() { checkForArrowKeys(3); getMouse(); //shottimer--; setRotation((int)getAngle()); } public void checkForArrowKeys( int adjust) { if ( Greenfoot.isKeyDown("a") ) { setLocation(getX() - adjust, getY() ); //setRotation(0); } if ( Greenfoot.isKeyDown("d") ) { setLocation(getX() + adjust, getY() ); //setRotation(180); } if ( Greenfoot.isKeyDown("w") ) { setLocation(getX(), getY() -adjust ); //setRotation(90); } if ( Greenfoot.isKeyDown("s") ) { setLocation(getX() , getY() + adjust ); //setRotation(270); } if (! Greenfoot.isKeyDown("space") ) { ifKeyIsStillDown = false; } if ( Greenfoot.isKeyDown("space") ) { shoot(); ifKeyIsStillDown = true ; } } public void shoot() { { if (!ifKeyIsStillDown) //if (shottimer < 1) { Bullet b = new Bullet(); b.setRotation(getRotation()); getWorld().addObject(b, getX(), getY()); //shottimer = 10; } } } public void getMouse() { MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse != null) { mX = mouse.getX(); mY = mouse.getY(); } } public double getAngle() { //double hyp = Math.sqrt(Math.pow(getX() - mX, 2) + Math.pow(getY() - mY, 2)); //double opp = Math.abs(getX() - mX); //System.out.println(hyp / opp); return Math.atan2(getY() - mY, getX() - mX) * 180.0 / Math.PI; } }
csr1010 csr1010

2013/11/22

#
I made it so that the bullet travels towards the mouse but is till need to figure out what the mouse's button is.
danpost danpost

2013/11/22

#
Use the Greenfoot class method 'mouseClicked(null)' to determine if the mouse was clicked and, if it was, create a new bullet (or shoot).
danpost danpost

2013/11/22

#
If you only want a shot to be created when a specific button of the mouse is clicked, you will need to get a MouseInfo object by using the Greenfoot class method 'getMouseInfo' and then the MouseInfo class method 'getButton' and compare that to the button you are wanting to use. Refer to the MouseInfo class documentation for information on how the buttons are numbered.
csr1010 csr1010

2013/11/25

#
I cannot find what the left mouse button is named.
danpost danpost

2013/11/25

#
Class MouseInfo documentation wrote...
getButton public int getButton() The number of the pressed or clicked button (if any). Returns: The button number. Usually 1 is the left button, 2 is the middle button and 3 is the right button.
Look at the last line.
csr1010 csr1010

2013/11/26

#
thank you, i have an issue with it still. When i click it seems to send more than one. I believe that it is not the code but it is the mouse, when you move it it detects a "click" and shoots even when you arent actually clicking, i have tried many different mice and it still happens. Should i just make a small shot timer to fix this? thank you
danpost danpost

2013/11/26

#
It sends more than one. Whatever that means. Please show the method that checks for the left mouse button and 'sends' whatever it is sending.
csr1010 csr1010

2013/12/3

#
Let me preface this by saying that I've been a memer since they first appeared on 4chan circa 2006 (ages ago in internet time). I remember refreshing /b/ all afternoon with the hope that someone would make a new meme. I was a memer back when Advice Dog was the only one and I still upvote every Advice Dog meme I see becasue of the fond rush of nostalgia it brings me. Nowadays, there are many thousand on quickmeme and other meme websites. I was a memer back before memegenerator was created (the first meme website) and I had to make them with MS Powerpoint or Paint. Speaking of meme websites, I was one of the first submitters to memebase.com and still have one of the top accounts there despite having migrated to reddit nearly 2 years ago. It was on 4chan and memebase where I cut my teeth creating memes, way before I had a reddit account and way before /r/adviceanimals was created in late 2010. Back before I could get any sort of points or even username recognition, I was creating memes as a clever and easily digestible way to reflect on society, relate some story to my audience, or just be funny. Do you remember rich raven? No? I do. You probably don't remember depression dog, crazy girlfriend praying mantis, introspective pug, or friendzone Johnny either. I remember all of them. In fact, you have only submitted two posts to adviceanimals garnering a total of just 4 points and have not commented there any time recently. So please, respect my judgement regarding the direction of the subreddit and the integrity of posts I have held dear to my heart for nearly 7 years but you have no strong feelings for. Thanks.
csr1010 csr1010

2013/12/3

#
Thanks
csr1010 csr1010

2013/12/3

#
I'm sorry about that. I left my computer open at school and my friend posted that. Ignore it.
There are more replies on the next page.
1
2