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

2020/12/11

Greenfoot shooting

Xarloz Xarloz

2020/12/11

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class survivor here. * * @author (your name) * @version (a version number or a date) */ public class Surviver extends Mover { private GreenfootImage image1; private GreenfootImage image2; /** * 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() { checkKeypress(); switchImage(); image1 = new GreenfootImage("Soldat.png"); image2 = new GreenfootImage("Soldat2.png"); if (touch(Zombie1.class)) {AllgCode.Leben--; destroy(Zombie1.class); } if (touch(Zombie2.class)) {AllgCode.Leben--; destroy(Zombie2.class); } } public void checkKeypress() { if (Greenfoot.isKeyDown("a")) { move(-10); } if (Greenfoot.isKeyDown("d")) { move(10); } if(Greenfoot.isKeyDown("w")) { setLocation(getX(),getY()-10); } if(Greenfoot.isKeyDown("s")) { setLocation(getX(),getY()+10); } if ("space".equals(Greenfoot.getKey())) { fire(); } } private void fire() { Projectile projectile = new Projectile(); getWorld().addObject(projectile, getX(), getY()); projectile.setRotation(getRotation()); } public void switchImage() { if (Greenfoot.isKeyDown("a")) { setImage(image1); } if (Greenfoot.isKeyDown("d")) { setImage(image2); } } } I want that the survivor shoots in the way , where he is looking. So if hegoes to the right, he also should shoot to the right. If he goes to the left, he also should shoot to the left.
danpost danpost

2020/12/11

#
Well, you cannot use rotation of survivor as it is always zero. You cannot use the set image as you are creating new ones every act cycle. That, in itself is not good as image processing is a hog on CPU time. I noticed, also, that you are allowing 8-way movement, but only use 2 images. Your actor only looks left or right. Are those the only directions allowed for a projectile?
You need to login to post a reply.