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

2014/3/16

Help with Shooting

patrick26 patrick26

2014/3/16

#
Hi, My actor Bowser is unable to shoot a fireball in the direction it is facing. Does anyone know how to do this? Please note that the image 'fireball1.png' is facing right and I have another image called 'fireball2.png' facing left. Here is my Bowser code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Fireball here. * * @author (your name) * @version (a version number or a date) */ public class Fireball extends Actor { private static final int SPEED=10; /** * Act - do whatever the Fireball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() {move(SPEED); if(getX()>getWorld().getWidth()-3) { getWorld().removeObject(this); } else { Actor kingboo = getOneIntersectingObject(Kingboo.class); if(kingboo !=null) { getWorld().addObject(new Explosion(),getX(),getY()); getWorld().removeObject(kingboo); getWorld().removeObject(this); } } } public Fireball() { setImage("fireball1.png"); } } Here is my Fireball code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Fireball here. * * @author (your name) * @version (a version number or a date) */ public class Fireball extends Actor { private static final int SPEED=10; /** * Act - do whatever the Fireball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() {move(SPEED); if(getX()>getWorld().getWidth()-3) { getWorld().removeObject(this); } else { Actor kingboo = getOneIntersectingObject(Kingboo.class); if(kingboo !=null) { getWorld().addObject(new Explosion(),getX(),getY()); getWorld().removeObject(kingboo); getWorld().removeObject(this); } } } public Fireball() { setImage("fireball1.png"); } }
danpost danpost

2014/3/17

#
Instead of a Bowser and a Fireball class, you posted the Fireball class twice. I believe the code that needs adjusted resides in the Bowser class.
patrick26 patrick26

2014/3/17

#
Here is the Bowser class: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bowser here. * * @author (your name) * @version (a version number or a date) */ public class Bowser extends Actor { private int vSpeed = 0; private int acceleration = 2; private boolean firing = false; private int coins = 0; private GreenfootImage image1 = new GreenfootImage("Bowser1.png"); private GreenfootImage image2 = new GreenfootImage("Bowser2.png"); private GreenfootImage image3 = new GreenfootImage("Bowser3.png"); private GreenfootImage image4 = new GreenfootImage("Bowser4.png"); public void act() { checkFall(); if(Greenfoot.isKeyDown("left")) { move(-7); if(getImage()==image3) { setImage(image4); setRotation(360); }else { setImage(image3); setRotation(360); } } if(Greenfoot.isKeyDown("right")) { move(7); if(getImage()==image1) { setImage(image2); setRotation(0); }else { setImage(image1); setRotation(0); } } if(Greenfoot.isKeyDown("up")) { jump(); } if(Greenfoot.isKeyDown("space")) { if(!firing){ getWorld().addObject(new Fireball(), getX(),getY()); Greenfoot.playSound("fireball.wav"); firing = true; } }else { firing = false; } Actor kingboo = getOneIntersectingObject(Kingboo.class); if(kingboo !=null) { getWorld().addObject(new Whitepoof(),getX(),getY()); getWorld().removeObject(this); } } public void jump() { vSpeed = -5; fall(); } public void fall() {setLocation(getX(),getY()+vSpeed); vSpeed = vSpeed + acceleration; } public boolean onGround() {Actor under = getOneObjectAtOffset(0, getY()-1105, Ground.class); return under !=null; } public void checkFall() {if(onGround()) { vSpeed = 0; }else{fall(); } } public void foundCoin() {coins = coins+1; if(coins==8){ Greenfoot.playSound("fanfare.wav"); Greenfoot.stop(); } } }
danpost danpost

2014/3/17

#
It would do good to have fields in both of these classes to track the horizontal direction of movement of these actors. 'setRotation(360)' is equivalent to 'setRotation(0)' -- therefore, all four of your 'setRotation statements in the 'act' method of Bowser can be removed.
You need to login to post a reply.