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

2016/12/15

If Statement Bewilderment -

Laurence Laurence

2016/12/15

#
import greenfoot.*; 
 
public class Kasu_Marie extends Actor
{
private GifImage gifR1 = new GifImage("Run.gif");
private GifImage gifL1 = new GifImage("RunBack.gif");
private GifImage gifR0 = new GifImage("Idle.gif");
private GifImage gifL0 = new GifImage("IdleBack.gif");
private GifImage gif = gifR0; 
public Kasu_Marie()
{
    int scalePercent = 30;    
    scaleGif(gifR1, scalePercent);
    scaleGif(gifR0, scalePercent);
    scaleGif(gifL1, scalePercent);
    scaleGif(gifL0, scalePercent);
}
private void scaleGif(GifImage gif, int scalePct)
{
    for (GreenfootImage image : gif.getImages())
    {
        int wide = image.getWidth()*scalePct/100;
        int high = image.getHeight()*scalePct/100;
        image.scale(wide, high);
    }
}
public void act() 
{
if (Level1.SequenceDone == true)
    {
    int dx = 0;
    if (Greenfoot.isKeyDown("d")) dx++;
    if (Greenfoot.isKeyDown("a")) dx--;
    move(3*dx);

    if (dx != 0) 
    {
        if (dx == 1) gif = gifR1; else gif = gifL1;
    }
    else
    {
        if (gif == gifR1) gif = gifR0;
        if (gif == gifL1) gif = gifL0;
    }
    setImage(gif.getCurrentImage());
}
}
}
When the If statement that is "if (Level1.SequenceDone == true)" is waiting to be activated, this affects the entire code which includes the resizing of the actor and makes things kinda awkward. In general, it works but I am trying to find a way for the resizing part of the code to stay active even when the If statement isn't provoked. Thanks in advance and User Corvus for the Global Variable idea!
danpost danpost

2016/12/15

#
It may be that the link between what images are brought in by the getImages method is broken when you scale the image. Try removing the scaleGif method from the Kasu_Marie class and add the following in the GifImage class:
public void scale(int pct)
{
    for (GreenfootImage image : images)
    {
        int wide = image.getWidth()*pct/100;
        int high = images.getHeight()*pct/100;
        image.scale(wide, high);
    }
}
Then change lines 13 through 16 in the Kasu_Marie class to:
gifR0.scale(scalePercent);
gifR1.scale(scalePercent);
gifL0.scale(scalePercent);
gifL1.scale(scalePercent);
Laurence Laurence

2016/12/15

#
It cannot find the method 'getHeight' once I place it within the GifImage class..? Had to change images to 'image'.
import greenfoot.*; 
 
public class Kasu_Marie extends Actor
{
private GifImage gifR1 = new GifImage("Run.gif");
private GifImage gifL1 = new GifImage("RunBack.gif");
private GifImage gifR0 = new GifImage("Idle.gif");
private GifImage gifL0 = new GifImage("IdleBack.gif");
private GifImage gif = gifR0; 
public Kasu_Marie()
{
    int scalePercent = 30;    
    gifR0.scale(scalePercent);
    gifR1.scale(scalePercent);
    gifL0.scale(scalePercent);
    gifL1.scale(scalePercent);
}
public void act() 
{
if (Level1.SequenceDone == true)
    {
    int dx = 0;
    if (Greenfoot.isKeyDown("d")) dx++;
    if (Greenfoot.isKeyDown("a")) dx--;
    move(3*dx);

    if (dx != 0) 
    {
        if (dx == 1) gif = gifR1; else gif = gifL1;
    }
    else
    {
        if (gif == gifR1) gif = gifR0;
        if (gif == gifL1) gif = gifL0;
    }
    setImage(gif.getCurrentImage());
}
}
}

The same problem seems present.
danpost danpost

2016/12/15

#
Since the resizing is done in the constructor, not within the 'if' block, I cannot see why scaling would be affected. I moved the scaling to within the GifImage class just to be sure that passing the images via the parameter of the 'getImages' method does not lose their link to the actual images (I did not really think it would be different; but, wanted to be sure. However, once the constructor sizes the gifs, they should not revert back to original size. Since the constructor is executed before the actor is placed into any world, the actor should take on those scaled images throughout it existence. To attempt to assist you further, maybe you can upload, with source, a simple scenario with the Kasu_Marie class and the GifImage class along with a simple world subclass. That way the gif files will be available for testing along with your current code.
Laurence Laurence

2016/12/15

#
I am not sure I understand your request there. You want myself to upload the code with just the world, Kasu_Marie and Gif Image classes?
danpost danpost

2016/12/15

#
Laurence wrote...
I am not sure I understand your request there. You want myself to upload the code with just the world, Kasu_Marie and Gif Image classes?
I am saying to create a new scenario and copy the codes of the Kasu_Marie and GifImage classes into it. Then create a simple World subclass. Finally import all the images into the images folder of the new scenario and upload it here including the source code. However, if you do not mind, or if that is about all that you have so far, you could just upload it as it is (again, with source code).
Laurence Laurence

2016/12/16

#
How do I upload scenarios to this site? I haven't done such before.
mkramer mkramer

2016/12/16

#
Laurence wrote...
How do I upload scenarios to this site? I haven't done such before.
On the upper right side of the Greenfoot environment (right above the Actor and World classes), there is a button showing a framed picture on an easel together with the word "Share . . .". You then have the options to "Publish" it on this site, export it as a "Webpage", or generate your own "Application" or "Project" (as Greenfoot archive file ".gfar"). Easiest for this community to check what's going on is the option "Publish". Therefore fill out all necessary fields, check "Publish source code" and enter your login UN/PW.
Laurence Laurence

2016/12/19

#
http://www.greenfoot.org/scenarios/18490 Scenario as requested
danpost danpost

2016/12/19

#
Try adding the following line at the end of the Kasu_Marie constructor (insert at line 17 above):
setImage(gif.getCurrentImage());
Laurence Laurence

2016/12/23

#
public Kasu_Marie()
{
    if (Level1.SequenceDone == false && true)
    {
    int scalePercent = 30;    
    gifR0.scale(scalePercent);
    gifR1.scale(scalePercent);
    gifL0.scale(scalePercent);
    gifL1.scale(scalePercent);
    setImage(gif.getCurrentImage());
}
This fixed it somehow, thanks for all the help provided throughout once more!
You need to login to post a reply.