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

2018/5/6

How to get bullet to fire in the direction your looking at.

OfficerRiot OfficerRiot

2018/5/6

#
So I've watched many videos on this and looked on here for anything cant seem to get it to work here is the code Any Help would be amazing! import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class RifleTrooper here. * * @author (your name) * @version (a version number or a date) */ public class RifleTrooper extends Actor { private GreenfootImage walk1 = new GreenfootImage("RifleTrooperWalk1.png"); private GreenfootImage walk2 = new GreenfootImage("RifleTrooperWalk2.png"); private GreenfootImage walk3 = new GreenfootImage("RifleTrooperWalk3.png"); private GreenfootImage walk4 = new GreenfootImage("RifleTrooperWalk4.png"); private GreenfootImage walk5 = new GreenfootImage("RifleTrooperWalk5.png"); private GreenfootImage walk6 = new GreenfootImage("RifleTrooperWalk6.png"); private GreenfootImage walk7 = new GreenfootImage("RifleTrooperWalk7.png"); private GreenfootImage walk1left = new GreenfootImage("RifleTrooperWalk1Left.png"); private GreenfootImage walk2left = new GreenfootImage("RifleTrooperWalk2Left.png"); private GreenfootImage walk3left = new GreenfootImage("RifleTrooperWalk3Left.png"); private GreenfootImage walk4left = new GreenfootImage("RifleTrooperWalk4Left.png"); private GreenfootImage walk5left = new GreenfootImage("RifleTrooperWalk5LEft.png"); private GreenfootImage walk6left = new GreenfootImage("RifleTrooperWalk6Left.png"); private GreenfootImage walk7left = new GreenfootImage("RifleTrooperWalk7Left.png"); private int frame; private int imageCounter; private int gunReloadTime; private int reloadDelayCount; private int shotsFired; private int bulletsfired; private int vSpeed = 0; private int acceleration = 2; private int jumpStrength = 8; private int timer = 0; private boolean isDown = false; /** * Act - do whatever the RifleTrooper wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { { forward(); reload(); checkFall(); inAir(); } } public void inAir() { if (isTouching(Platform.class)) { jumping(); } } public void checkFall() { if(onGround()) { vSpeed = 0; } else { fall(); } } public boolean onGround() { Actor under = getOneObjectAtOffset ( 0, getImage().getHeight() / 2, Platform.class); return under != null; } public void fall() { setLocation (getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } private void reload() { String key = Greenfoot.getKey(); if (key != null && key.equals("r")) { if (bulletsfired == 201) { bulletsfired = 0; } } } /** * initialises shooty thing */ public RifleTrooper() { gunReloadTime = -1000; reloadDelayCount = 0; shotsFired = 0; } /** * Return the number of shots fired from this trooper. */ public int getShotsFired() { return shotsFired; } /** * Set the time needed for re-loading the rocket's gun. The shorter this time is, * the faster the rocket can fire. The (initial) standard time is 20. */ public void setGunReloadTime(int reloadTime) { gunReloadTime = reloadTime; } /** * Moves Dooku * */ public void forward() { if (Greenfoot.isKeyDown("d")) { move(2); imageCounter = (++imageCounter)%6; // change '2' higher to slow down animation if (imageCounter == 0) switchImageright(); } if (Greenfoot.isKeyDown("a")) { move(-2); imageCounter = (++imageCounter)%6; // change '2' higher to slow down animation if (imageCounter == 0) switchImageleft(); } if ("k".equals(Greenfoot.getKey())) { fire(); } } public void jumping() { if(Greenfoot.isKeyDown("space")) { jump(); } } public void jump() { vSpeed = -jumpStrength; } public void switchImageright() { frame++; switch (frame){ case 1: setImage(walk1); break; case 2: setImage(walk2); break; case 3: setImage(walk3); break; case 4: setImage(walk4); break; case 5: setImage(walk5); break; case 6: setImage(walk6); break; case 7: setImage(walk7); break; case 8: frame = 0; } } public void switchImageleft() { frame++; switch (frame){ case 1: setImage(walk1left); break; case 2: setImage(walk2left); break; case 3: setImage(walk3left); break; case 4: setImage(walk4left); break; case 5: setImage(walk5left); break; case 6: setImage(walk6left); break; case 7: setImage(walk7left); break; case 8: frame = 0; } } private void fire() { Bullet bullet = new Bullet(); getWorld().addObject(bullet, getX(), getY()); bullet.setRotation(getRotation()); } }
danpost danpost

2018/5/7

#
You should not use the getKey method for in-game actions -- it is mainly used for text string input. Use isKeyDown with a boolean to track the state of the key (so you can determine when the key changes state instead of just what state it currently is in). Currently, you are not retaining the direction your actor is facing. So, as is, the only way to determine it is to check to see if any of the 7 images in one direction is currently set to the actor.
OfficerRiot OfficerRiot

2018/5/7

#
danpost wrote...
You should not use the getKey method for in-game actions -- it is mainly used for text string input. Use isKeyDown with a boolean to track the state of the key (so you can determine when the key changes state instead of just what state it currently is in). Currently, you are not retaining the direction your actor is facing. So, as is, the only way to determine it is to check to see if any of the 7 images in one direction is currently set to the actor.
Ok, I understand what you're saying but I have no idea where to start on doing that do you have some example code that I could go off of?
danpost danpost

2018/5/7

#
OfficerRiot wrote...
Ok, I understand what you're saying but I have no idea where to start on doing that do you have some example code that I could go off of?
See here.
OfficerRiot OfficerRiot

2018/5/7

#
danpost wrote...
OfficerRiot wrote...
Ok, I understand what you're saying but I have no idea where to start on doing that do you have some example code that I could go off of?
See here.
Ok, I looked at that but a lot of it I could not understand. Can you give me a little more help?
You need to login to post a reply.