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

2014/1/17

problems with changing an image every 1Y a image moves down

UNDEAD_DC UNDEAD_DC

2014/1/17

#
Hi i want an image to change every time it moves 1 Y down (or 5 or 10 Y) so it is some kind of GIF without being a actual GIF image here is the code what i want is that the name of the image stays the same and every time the number is +1 so imagename imagename etc. when at imagename it has to go back to imagename
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Booster here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Booster extends Objects
{
    /**
     * Act - do whatever the Booster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        dropSpeedBooster();
        //spinningImage();
    }    


    
    public static int YB;
    public int getYPosition(){
      YB = getY();
      return YB;
    }
    
    
    public void spinningImage()
    {
            if (getWorld() != null) { 
                //int y1 = getY();
                //int a1=0;
                //int a2=Y-a1;       
                int a2 = getYPosition();
                if (a2>=21) 
                {
                    getWorld().addObject( new Booster(), ran+130, 0);
                }
            }
    }
      
}

danpost danpost

2014/1/17

#
Could you not just use the y-coordinate as a control:
if ((getY()/5)%2 == 0) setImage(image1); else setImage(image2);
And if you would rather not have the image set every act cycle, but only when it needs to change:
if (getY()%5 == 0)
{
    if ((getY()/5)%2 == 0) setImage(image1); else setImage(image2);
}
UNDEAD_DC UNDEAD_DC

2014/1/17

#
but this way i need to put for every image a setImage(image1)? that would be very much code because i have 20 images called DraaiMuntje so DraaiMuntje1 DraaiMuntje2 DraaiMuntje3 DraaiMuntje4 DraaiMuntje5 DraaiMuntje6 and so on till: DraaiMuntje20 and i would like to have it changed automaticly so when the Y changes the number behind DraaiMuntje does to
lordhershey lordhershey

2014/1/17

#
I load images like this when I have numbered images: /*the images are named fryguy1.png to fryguy27.png*/ GreenfootImage frying = new GreenfootImage; for(i = 0 ; i < 27 ; i++) { frying = new GreenfootImage("fryguy" + (i+1) + ".png"); } have a member variable called counter and in the Actor's act() do something like: counter = (counter +1) %27 setImage(frying);
danpost danpost

2014/1/17

#
How about simply:
setImage("DraaiMuntje"+(getY()%20+1));
UNDEAD_DC UNDEAD_DC

2014/1/17

#
danpost wrote...
How about simply:
setImage("DraaiMuntje"+(getY()%20+1));
that gives me
java.lang.IllegalArgumentException: java.io.FileNotFoundException: Could not find file: DraaiMuntje16
	at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:293)
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:109)
	at greenfoot.Actor.setImage(Actor.java:397)
	at Booster.act(Booster.java:20)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
Caused by: java.io.FileNotFoundException: Could not find file: DraaiMuntje16
	at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:537)
	at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:290)
	... 7 more
now going to try lordhershey's version
lordhershey lordhershey

2014/1/17

#
I think you have a file missing - that one missing file caused that waterfall of exceptions, check the file DraaiMuntje16 to see if it is mis-spelled or may have an extra characters.
danpost danpost

2014/1/17

#
Just make sure your files are named properly and all of them are in your images folder. Also, I did not add the file extension in the setImage line ( presumably ' + ".png" ' ).
UNDEAD_DC UNDEAD_DC

2014/1/17

#
UNDEAD_DC wrote...
danpost wrote...
How about simply:
setImage("DraaiMuntje"+(getY()%20+1));
that gives me
java.lang.IllegalArgumentException: java.io.FileNotFoundException: Could not find file: DraaiMuntje16
	at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:293)
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:109)
	at greenfoot.Actor.setImage(Actor.java:397)
	at Booster.act(Booster.java:20)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
Caused by: java.io.FileNotFoundException: Could not find file: DraaiMuntje16
	at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:537)
	at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:290)
	... 7 more
now going to try lordhershey's version
Nevermind had to put + ".png" in this> ("DraaiMuntje"+(getY()%20+1)); ("DraaiMuntje"+(getY()%20+1)+ ".png");
UNDEAD_DC UNDEAD_DC

2014/1/17

#
Thank you so much, another question: my coin is now spinning VERY fast how to slow it down? not the move speed but the change speed of wich the pictures change?
danpost danpost

2014/1/17

#
setImage("DraaiMuntje"+((getY()/2)%20+1)+".png");
You can use any fractional value to multiply by (instead of '1/2 speed'). For '2/5 speed':
setImage("DraaiMuntje"+((getY()*2/5)%20+1)+".png");
You need to login to post a reply.