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

2012/8/31

example of dynamic sprite drawing?

dangjavageek dangjavageek

2012/8/31

#
Is there a pointer to a greenfoot scenario somewhere that uses graphics to draw the sprite dynamically, every time act is called? I've tried a couple of runs at this and something seems to be redrawing the original sprite image over and over again so that I see the custom graphics before the scenario starts but not after it starts. Something like this: ---8<---8<---8<--- MyActorIsNotWorking.java ---8<---8<---8<--- public void act() { // Add your action code here. drawShip(); checkForKeyboardEvents(); checkForWorldEdge(); } public void drawShip() { GreenfootImage original = getImage(); GreenfootImage image = new GreenfootImage(original.getWidth(), original.getHeight()); int diameter = Greenfoot.getRandomNumber(30); int x = Greenfoot.getRandomNumber(image.getWidth()); int y = Greenfoot.getRandomNumber(image.getHeight()); int r = Greenfoot.getRandomNumber(256); int g = Greenfoot.getRandomNumber(256); int b = Greenfoot.getRandomNumber(256); Color c = new Color(r, g, b); image.clear(); image.setColor(c); image.setTransparency(128); image.fillOval(0, 0, diameter, diameter); image.drawLine(0, 0, 10, 10); image.drawLine(0, 0, 0, 10); image.drawLine(0, 0, 10, 0); image.drawLine(10, 10, 0, 10); image.drawLine(10, 10, 10, 0); image.drawLine(10, 10, 10, 0); setImage(image); } ---8<---8<---8<--- MyActorIsNotWorking.java ---8<---8<---8<--- I'm not finding much on techniques for this in the greenfoot book or in the discussion forums. I would love to start students on their own scenarios making their own little versions of a game like Captain Forever (http://www.captainforever.com/captainforever.php) or derivatives but I'm not out of the starting block doing anything besides displaying images for a sprite. Any thoughts? Thanks. Best regards, -- DanG
danpost danpost

2012/8/31

#
I do not know what you are using for the original image that you initially get with 'getImage()', but I started with several different sized images and my results were what was expected. A small square with a cross and a flickering, size-changing, color-changing circle. I do not know if you are wanting to slow the changing of it down or what, but the code does work (for what it says to do). Maybe you wanted to do something with x and y, as you declared them without using them.
danpost danpost

2012/8/31

#
Or, perhaps you are just wanting the originally drawn image to be in a steady-state. For that, just move 'drawShip();' (the call in the act method) to the constructor, so the image is not re-drawn every act (just when the Ship object is created).
dangjavageek dangjavageek

2012/8/31

#
Thanks, danpost! I posted the scenario to http://www.greenfoot.org/scenarios/5863 and what I'm trying to accomplish so that students can do their own custom animation based on an example I'd provide, is to have the actor be updated by the drawing commands each act() and not show up correctly before you hit the button but then be replaced by the original image forever after the scenario is started. This is the opposite of your last question, i.e., I _do_ want drawShip() or some other customer drawing routine called every act() invocation and specifically _not_ the original image while the scenario is running. If you play around with the captain forever game (http://www.captainforever.com/captainforever.php) your little one block ship you start with is pulsing and generally animating the whole time. That's what I want the students to play with, but for now, I'm not getting how to tell the actor to redraw itself repeatedly, currently it shows up ok before the scenario is run but then sticks to the original image associated with the sprite for the entire run of the scenario. Thanks for the quick response! Best regards, -- DanG
danpost danpost

2012/8/31

#
I'll take a look at it and see what I can find.
danpost danpost

2012/8/31

#
In your SpaceShip class, remove line 10. There is no sense in holding that as an instance or class variable. Change your SpaceShip constructor to:
public Spaceship ()
{
    setImage(new GreenfootImage(30, 30));
}
That will keep your ship from appearing until 'Run' is clicked, when the act cycle will change the image. OK, the main reason your image is not changing is in your keycheck for the 'up' key you are changing the image right back.
danpost danpost

2012/8/31

#
Change the order of action within your act method. Check for keys first; then, mess with the image.
dangjavageek dangjavageek

2012/8/31

#
ah, that's it. Thanks!
You need to login to post a reply.