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

2017/3/26

Shoot in every direction

Malmi Malmi

2017/3/26

#
So I´m still new in Greenfoot. So my question is how I can shoot in every direction (N,S,W,E). So the thin gis that i want the Player(Spieler) to shoot in the direction he is looking to (the iven Image). So now my question is can I request the Image of the player in the Bullet(Geschoss).class. Can someone help me ;D?
Super_Hippo Super_Hippo

2017/3/26

#
You do not need the player image in the bullet class. The bullet should just move straight and checks for intersection. When creating the bullet, simply set the rotation to the rotation of the player:
Actor bullet = new Bullet();
getWorld().addObject(bullet, getX(), getY());
bullet.setRotation(getRotation());
Malmi Malmi

2017/3/26

#
But the thing is that my Player hasn`t got any Rotation. I just want the Bullet in the Direction to which the Image belongs to. So when the Image for my Player is "Player_left.png" then i want (in the bulletclass) the bullet to getaRotation form 180
danpost danpost

2017/3/26

#
The player must know what direction he is facing (or at least its forward direction) -- just change the third line to suit. If you do not understand, show the class code for the player.
Malmi Malmi

2017/3/26

#
okay. I don`t quite understand and might you be able and help me with another discussen. It is about Player and wall. I posted it today aswell.
public class Spieler extends Actor
{
   int speed=2;
   int timer=0;
   private Counter_Punkte counter;
   public Spieler(Counter_Punkte PunkteCounter)
   {
       counter = PunkteCounter;
    }
   /**
     * Act - do whatever the Wall wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
   public void act()
    {
        bewegung();      
        Finalcheck();  
        Schuss();
        //leertasteGedrueckt = false;
    }    
   public void bewegung()
   {
        if (Greenfoot.isKeyDown("a"))
        {
            setImage("Player_links.png"); 
            setLocation(getX()-speed, getY());
            Greenfoot.delay(1);
            setImage("Player_startli.png");
        }
        if (Greenfoot.isKeyDown("w"))
        {
            setImage("Player_rücken.png"); 
            setLocation(getX(), getY()-speed);
            Greenfoot.delay(1);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            setImage("Player_rechts.png"); 
            setLocation(getX()+speed, getY());
            Greenfoot.delay(1);
            setImage("Player_startre.png");
        }
        if (Greenfoot.isKeyDown("s"))
        {
           setImage("Player_vorne.png"); 
           setLocation(getX(), getY()+speed);
           Greenfoot.delay(1);
        }
    }
   public void Schuss()
    {
        this.timer = this.timer + 1;
        if (Greenfoot.isKeyDown("space")&& this.timer > 35)
        {
            this.getWorld().addObject(new Geschoss(), this.getX(), this.getY());
            this.timer = 0;
                       
        }
    }
    
   public boolean Spieler()
   {
       int SpHöhe = getImage().getHeight()+15;
       int SpBreite = getImage().getWidth()+15;
       return true;
   }
   public boolean Wand()
    {
        Object Spieler = getOneObjectAtOffset(getImage().getWidth()-30, getImage().getHeight()-30, Wand.class);
        return Spieler != null;
        
    }
   public void checkWandOben()
    {
        if (Wand())
        {
            setLocation(getX(), getY()+2*speed);
        }
        } 
   public void checkWandUnten()
    {
        if (Wand())
        {
            setLocation(getX(), getY()-2*speed); 
        }
        } 
   public void checkWandRechts()
    {
        if (Wand())
        {
            setLocation(getX()+2*speed, getY());
        }
        } 
   public void checkWandLinks()
    {
        if (Wand())
        {
            setLocation(getX()-2*speed, getY());
        }
        } 
   public void Finalcheck()
    {
        if (Greenfoot.isKeyDown("a"))
        {
            checkWandRechts();
        }
        if (Greenfoot.isKeyDown("w"))
        {
            checkWandOben();
        }
        if (Greenfoot.isKeyDown("d"))
        {
            checkWandLinks();
        }
        if (Greenfoot.isKeyDown("s"))
        {
           checkWandUnten();
        }
    }  
    
danpost danpost

2017/3/26

#
Add an 'int direction' field to the class and set it to 0, 90, 180, and 270 in the movement blocks (lines 24 through 49). Use Hippo's three-liner as a template to replace line 56. Use 'direction' instead of 'getRotation()' in the third line.
Malmi Malmi

2017/3/26

#
That was kinda easy ;D Thank you guys very much @danpost can you help me with this one aswell ;D. "Don`t use the Middlepoint of the Player while hitting the wall" (I want the Imagesize of my Player to be tacking as the Object that hits the wall. So not the middlepoint)
danpost danpost

2017/3/26

#
Malmi wrote...
I want the Imagesize of my Player to be tacking as the Object that hits the wall. So not the middlepoint
You should probably use the 'move-check-move back' procedure. Instead of trying to look ahead to see what is there, you blindly move and "stop" at anything you hit. You will need to either know where you moved from or the distances you tried to move along both the horizontal and the vertical. Then just use 'isTouching' instead of 'getOneObjectAtOffset'. This means removing all the code from line 62 to the end, changing the 'bewegung' method, and modifying the 'act' method. In the 'bewegung' method, start with the following line:
int dx = 0, dy = 0;
In each of the four movement blocks, adjust the value of one of these two variables as needed (for example, in the "w" block, decrement dy: 'dy--;'). After the fourth movement block add the following:
if (dx*dy != 0 || dx+dy == 0) return; // ensures only one direction is moved in, if any
setLocation(getX()+dx*2*speed, getY()+dy*2*speed); // move
if (isTouching(Wand.class)) setLocation(getX()-dx*2*speed, getY()-dy*2*speed); // move back if wall touched
In the act method, remove the call to 'FinalCheck'.
Malmi Malmi

2017/3/26

#
Is this right? and i tried something and now i keep moving through the walls.Maybe that is a problem because i wrote it wrong?
 if (Greenfoot.isKeyDown("w"))
        {
            setImage("Player_rücken.png"); 
            setLocation(getx()dx, getY()dy-speed);
            Greenfoot.delay(1);
            direction=270;
        }
danpost danpost

2017/3/26

#
Remove the 'setLocation' lines within the four key check blocks and replace them with the appropriate change in 'dx' or 'dy'. For example, in the block given above, change line 4 to:
dy--;
The actual moving will be done after the four blocks have executed.
Malmi Malmi

2017/3/26

#
okay that works NICE ;D
You need to login to post a reply.