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

2012/12/1

Image Change

BradH BradH

2012/12/1

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Knight1 here. * * @author (BradH) * @version (a version number or a date) */ public class Knight1 extends GoodTroops {private int Walk = (9); /** * Act - do whatever the Knight1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(2); Walk -- ; if(Walk == 0) { setImage("image-GoodKnight2.png"); } hello, I am trying to get my Knight1 class to change the Image to GoodKnight2.png once walk==0; GoodKnight2 is an image I have uploaded into my project, previously have just been removing my knight1 class from the world and adding a new class that has the GoodKnight2.png image and then removing that class and getting knight1 to appear again to give it an animated look. The problem with that is I can not make A death Image because the classes get removed from the world everytime walk == 0; Back to my main question in the code above when I run the game an error pops up saying that it can not find image GoodKnight2.png which is the name of the image that I have uploaded into my project, why can't it find this image. Thanks for your time.
Gevater_Tod4711 Gevater_Tod4711

2012/12/1

#
if you cant find this image the filename of it could be wrong or you could have saved it to a wrong place. You have to save the image in images in your scenario
BradH BradH

2012/12/1

#
I imported the image into my game it was a png I created in another program, it is listed as GoodKnight2 when I look at the setimages. I tried another image from the images imported in my game bit it also failed to work so maybe there is something wrong in my code.
danpost danpost

2012/12/1

#
Maybe you should share what code you tried and we could possibly help.
BradH BradH

2012/12/1

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Knight1 here. * * @author (your name) * @version (a version number or a date) */ public class Knight1 extends GoodTroops {private int Walk = (9); /** * Act - do whatever the Knight1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(2); Walk -- ; if(Walk == 0) { setImage("image-GoodKnight2.png"); } //units this class can kill Actor BadKNight2; BadKNight2 = getOneObjectAtOffset(0, 0, BadKNight2.class); if (BadKNight2 != null) { World world; world = getWorld(); world.removeObject(BadKNight2); } Actor BadKnight1; BadKnight1 = getOneObjectAtOffset(0, 0, BadKnight1.class); if (BadKnight1 != null) { World world; world = getWorld(); world.removeObject(BadKnight1); } Actor BadRam1; BadRam1 = getOneObjectAtOffset(0, 0, BadRam1.class); if (BadRam1 != null) { World world; world = getWorld(); world.removeObject(BadRam1); } Actor BadRam2; BadRam2 = getOneObjectAtOffset(0, 0, BadRam2.class); if (BadRam2 != null) { World world; world = getWorld(); world.removeObject(BadRam2); } //once knight1 hits world edge remove it if (atWorldEdge()) {World world; world = getWorld(); world.removeObject(this); } } } This is my code for my Knight1 class it complies fine but when I spawn this unit it can not find GoodKnight2.png (which is in my game under setImages) If it would be easier I could publish my source code for the game
danpost danpost

2012/12/1

#
Maybe you meant
setImage("GoodKnight2.png");
// instead of
setImage("image-GoodKnight2.png");
BradH BradH

2012/12/1

#
ok, thanks that worked I was watching a tutorial and I must of gotten mixed up, thanks
BradH BradH

2012/12/1

#
One more quick question how would I get Knight1 image to come back and keep on switching between the two, would I use a loop? To keep reverting back to the first Image then restarting over.
danpost danpost

2012/12/1

#
Make the following changes in the class
// change
private int Walk = (9);
// to
private int Walk = (0);
// add the following class array
private static final String[] IMAGES= { "GoodKnight1.png", "GoodKnight2.png" };
// add the following class int
private static final int RATE = 9; // adjust as neccessary
// Then, change the following
Walk -- ;
if(Walk == 0)
{
    setImage("image-GoodKnight2.png");
}    
// to this
Walk = (Walk+1)%(RATE*IMAGES.length);
if (Walk%RATE==0)
{
    setImage(IMAGES[Walk/RATE]);
}
This will animate your actor. You can list all the images you want in the array 'IMAGES', and even have the same image name in there more than once; as long as you list them in the order they are to appear. And the value of 'RATE' can be adjusted to suit the speed of your scenario (the initial value of 9 was retained in the code above).
BradH BradH

2012/12/1

#
public class Knight1 extends GoodTroops {private int Walk = (0); /** * Act - do whatever the Knight1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() {Walkin(); Killing(); } public void Walkin() { move(2); private static final String IMAGES= {"GoodKnight 1.png", "GoodKnight2.png" }; private static final int RATE = 9; Walk = (Walk+1)%(RATE*IMAGES.length); if (Walk%Rate==0) { setImage(IMAGES); } } Did I type something in the wrong place because an error comes up saying illegal start of expression at private static final String
vonmeth vonmeth

2012/12/2

#
private static final String[] IMAGES= {"GoodKnight 1.png", "GoodKnight2.png" };
private static final int RATE = 9;
Those need to moved up near where you have "private int Walk = (0);"
BradH BradH

2012/12/2

#
oh, thank you
You need to login to post a reply.