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

2017/12/4

Shooting: bullet does not move across screen

michelleed michelleed

2017/12/4

#
SO: I have this game thing that a friend and I are making for a class and we want our actor to shoot poison. The poison is appearing on the screen when the down key is pressed, which is what we want, except it just sort of sits where it was created. It doesn't move across the screen. Can anyone help with this? Poison class code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Poison extends Mover { /** A bullet loses one life each act, and will disappear when life = 0 */ public int life = 30; /** The damage this bullet will deal */ public int damage = 16; /** * Act - do whatever the Poison wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int direction, speed; private Vector movement = new Vector(); public void act() { if(life <= 0) { getWorld().removeObject(this); return; } else if(getX() <= 0) { move(); CatZombie yeet = (CatZombie) getOneIntersectingObject(CatZombie.class); if (yeet != null) { getWorld().removeObject(this); yeet.hit(damage); } else { life--; } setRotation(direction); setLocation(getX() + speed, getY()); move(50); checkBoundaries(); } } public Poison() { //make image smaller GreenfootImage image = getImage(); image.scale(image.getWidth()- 10, image.getHeight()-10); setImage(image); speed = 100; } public void checkBoundaries() { //make poison disappear if it gets off screen if(getX() > getWorld().getWidth() - 1) getWorld().removeObject(this); else if(getX() < 1) getWorld().removeObject(this); if(getY() > getWorld().getHeight() - 1) getWorld().removeObject(this); else if(getY() < 1) getWorld().removeObject(this); } } Actor code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class DogDoctor extends Mover { public boolean isDown = false; private int ySpeed; private int speed = 7; public int health = 20; public GreenfootImage image = getImage(); //sizes doctor to be smaller public DogDoctor() { //public GreenfootImage image = getImage(); image.scale(image.getWidth() - 100, image.getHeight()- 150); setImage(image); } public void act() { checkKeys(); checkFire(); move(1); //Jumping int groundLevel = 500; boolean onGround = (getY() == groundLevel); if (!onGround) // in middle of jump { ySpeed++; // adds gravity effect setLocation(getX(), getY()+ySpeed); // fall (rising slower or falling faster) if (getY()>=groundLevel) // has landed (reached ground level) { setLocation(getX(), groundLevel); // set on ground Greenfoot.getKey(); // clears any key pressed during jump } } else // on ground { if ("space".equals(Greenfoot.getKey())) // jump key detected { ySpeed = -25; // add jump speed setLocation(getX(), getY()+ySpeed); // leave ground } } //Cat kills dog if (isTouching(CatZombie.class)) { getWorld().removeObject(this); } } public void checkFire() { if(Greenfoot.isKeyDown("down")) { getWorld().addObject(new Poison(), getX(), getY()); } } private boolean checkKeys() { //move left if(Greenfoot.isKeyDown("left")) { setImage("TheDoctor-Left.png"); GreenfootImage doctorLeft = getImage(); doctorLeft.scale(image.getWidth(), doctorLeft.getHeight() - 150); setImage(doctorLeft); moveLeft(); } //move right if(Greenfoot.isKeyDown("right")) { setImage("TheDoctor-Right.png"); GreenfootImage doctorRight = getImage(); doctorRight.scale(image.getWidth(), doctorRight.getHeight() - 150); setImage(doctorRight); moveRight(); } return(isDown); } public void moveRight() { setLocation( getX() + speed, getY() ); } public void moveLeft() { setLocation( getX() - speed, getY() ); } public void hit(int damage) { life = life - damage; } }
danpost danpost

2017/12/4

#
Cannot get the full picture of what you have as the Poison class extends a Mover class which is not given here.
michelleed michelleed

2017/12/5

#
@danpost import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Mover here. * * @author (your name) * @version (a version number or a date) */ public abstract class Mover extends Vector { /** * Act - do whatever the Mover wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ /** A bullet looses one life each act, and will disappear when life = 0 */ public int life = 30; /** The damage this bullet will deal */ public int damage = 16; private Vector movement = new Vector(); private double x = 0; private double y = 0; public Mover() { } /** * Create new mover initialised with given speed vector. */ public Mover(Vector speed) { movement = speed; } /** * Move forward one step. Direction and speed are dtermined by the * internal movement vector. Movement wraps around at the world edges. */ public void move() { x = x + movement.getX(); y = y + movement.getY(); if(x >= getWorld().getWidth()) { x = 0; } if(x < 0) { x = getWorld().getWidth() - 1; } if(y >= getWorld().getHeight()) { y = 0; } if(y < 0) { y = getWorld().getHeight() - 1; } setLocation(x, y); } public void setLocation(double x, double y) { this.x = x; this.y = y; super.setLocation((int) x, (int) y); } public void setLocation(int x, int y) { setLocation((double) x, (double) y); } /** * Increase the current speed with the given vector. */ public void increaseSpeed(Vector s) { movement.add(s); } public void hit(int damage) { life = life - damage; } /** * Return the current movement. */ public Vector getMovement() { return movement; } //} } vector import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Vector here. * * @author (your name) * @version (a version number or a date) */ public class Vector extends Actor { double dx = 0; double dy = 0; int direction = 0; double length; /** * Create a default vector initialised to zero. */ public Vector() { } /** * Create a vector with given direction and length. */ public Vector(int direction, double length) { this.length = length; this.direction = direction; dx = length * Math.cos(Math.toRadians(direction)); dy = length * Math.sin(Math.toRadians(direction)); } /** * Set the direction of this vector. */ public void setDirection(int direction) { this.direction = direction; dx = length * Math.cos(Math.toRadians(direction)); dy = length * Math.sin(Math.toRadians(direction)); } /** * Add another vector to this vector. */ public void add(Vector other) { dx += other.dx; dy += other.dy; this.direction = (int) Math.toDegrees(Math.atan2(dy, dx)); this.length = Math.sqrt(dx*dx+dy*dy); } /** * Return the x offset of this vector. */ /* public double getX() { return (double) dx; } /** * Return the y offset of this vector. */ /*public int getY() { return dy; }*/ /** * Return the current direction (in degrees). */ public int getDirection() { return direction; } /** * Return the current length of the vector. */ public double getLength() { return length; } /** * Create a copy of this vector. */ public Vector copy() { Vector copy = new Vector(); copy.dx = dx; copy.dy = dy; copy.direction = direction; copy.length = length; return copy; } } The mover class and vector were things used in other Greenfoot example scenarios from Greenfoot
danpost danpost

2017/12/5

#
I do not think the Vector class should extend the Actor class -- a vector is NOT an actor (I doubt you saw that in any scenario examples). A vector is an object with two values -- an angle and a length, which is what are needed to describe a vector. The Mover class creates a Vector object to describe the current direction and speed (or 'movement') of the actor. The Mover class should extend the Actor class and the Vector class should extend the Object class (this is implied if no 'extends' clause is given in the class declaration line). Next question: At the beginning of your world constructor, what exactly are you using for the 'super' line?
You need to login to post a reply.