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

2017/4/25

change image when object gets hit

vbh vbh

2017/4/25

#
Hi im making a game where balls comes down from the top and get shot by a bullet. When it gets shot i want to change the current image to another. but cant find a soulotion this is the code i have. i have a PinkBall class and a Bullet class, hope someone can help. public class Bullet extends Animal { private GreenfootImage pinkSplach; public Bullet() { pinkSplach = new GreenfootImage("pinkSplach.png"); } public void act() { hitBall(); } public void hitBall() { Actor actor = getOneIntersectingObject (PinkBall.class); PinkBall pinkBall = new PinkBall(); if (actor != null) { World world = (World)getWorld(); pinkBall.setImage(pinkSplach); world.removeObject (actor); world.removeObject (this); }
Super_Hippo Super_Hippo

2017/4/25

#
1. Bullet should not extend animal 2. the bullet should not save an image which you want to give to the pink ball 3. in the hitBall method, you first get a reference to the pink ball the bullet is touching. In the next line, you create a new pink ball object and later you set a new image to this new pink ball and not to the one in the world. And then, you remove the bullet and the pink ball. You should set the image of the 'actor' instead of removing it from the world.
vbh vbh

2017/4/26

#
ty for replay :-) its working now
You need to login to post a reply.