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

2012/3/10

PyroTyro4.1 difficulties

dlanni dlanni

2012/3/10

#
I am a beginner and working through the book with class (we are on chap 5). My problems are: 1. I can't get the houses to recognize image2 (trunk up) of Firefighting elephant, so have settled for having the fires put out with main image (trunk down). 2. I have stuck delays into my on fire animation because it goes so quickly as to be unrecognizable otherwise, but this slows down all of the characters. 3. For game loss, I wanted to set up a counter of image 1 houses (not burnt) and if it got to < 3, (only two houses unburnt currently), Greenfoot.stop., but I don't know which method counts specific objects or where I would place it (in my World?). 4. I want to attach a guttering flame animation to the tip of the match as a separate object that would travel with the match and stay in its proper place (at end of match), so that when elephant controller goes to blow out match, blowing out the non-fire end of match would not work--player would have to aim at the flame. Maybe this is too complicated and I should just leave the match as all one piece, but can it be done? (have two objects travel randomly together?) 5. In general, I am wondering where I can put code that involves the interaction of specific images (not the original "setImage=image1", but some later image) from more than one class. Can we write act into World?
danpost danpost

2012/3/10

#
In reverse order: (5) Yes, you can write an act() method for the World; however, when working with interaction between actors, it is usually best to write the code into one the actor classes. (4) It would not be too difficult to write code for one actor to follow another, the one object can continually set the location of the other (create the object that will follow first, getting a reference to it; then, create the object that controls it, sending it the reference to the other object and having the constructor (with an actor parameter) save the reference in an instance variable). Can provide more details on this later, if need be. (3) It makes no difference counting houses burning or houses not burning, as long as the check is set up correctly. The code for this would most easily go in the House class (not the Pyro-Tyro class) by setting up a static class variable with
private static int housesBurning = 0;
The static modifier makes this variable a class variable, not an object variable (do you remember the bicycle class in the tutorial? or did you see it?). Anyway, by declaring the variable static, there will only be one variable for the class, not one for each instance object of that class. Anytime an instance of a house is put ablaze or doused out, adjust the value of this variable accordingly, create a method to check its value for game over and add a call to it in the act() method. (2) Animation can be much more easily controlled by using an array for the images used and having a instance integer variable set to which element in the array the current image is. Instead of using a 'delay' statement, use a countdown instance integer variable. On each act(), decrease the count; if zero, go to next image and reset the count. Again, more details can be provided later. (1) Finally, when the 'up' key is pressed, if intersecting a house, call a (new) method in the House class telling the house it is being doused. The (new) method should make sure it is on fire by checking the image number (as set up in (2) above), and if it is on fire, reset the image to non-burning image and decrease the housesBurning variable. I hope this helped. Please post for specifics if needed.
dlanni dlanni

2012/3/11

#
It is great to know that these problems can be solved. My problem is that the depth of my understanding vocabulary of Java/Greenfoot is very shallow. I've watched all of Mik's tutorials but am unaware of others. Where is the bicycle class tutorial? The first thing I will work on is moving the animation into an array, since we just started that chapter (5) and I need a LOT of experience with the subject before it becomes second nature. Now I will try to figure out how to write the code. The only sample I have is the piano scenario. What does incrementing the act with a countdown look like? Are there any other tutorials around?
danpost danpost

2012/3/11

#
Actually, it would be decrementing a variable in the act() method, and it would look something like this:
countdown--; // would be initially set to a maxCount of maybe 25 or so
if (countdown == 0)
{
    imageNum++; // increment index
    if (imageNum > maxImages) imageNum = 1; // recycle if past end of list
    // went back to 1, not 0, as 0 would be a non-burning house image
    setImage(image[imageNum]);
    countdown = maxCount;
}
There is a little trick that can be used, but the non-burning house image would have to be the last image in the list of images.
if (countdown == 0)
{
    imageNum = (imageNum + 1) % (maxImages - 1); // increments AND recycles image index
    setImage(image[imageNum]);
    countdown = maxCount;
}
I'll re-locate the bicycle tutorial and post back.
danpost danpost

2012/3/11

#
I found the tutorial with the bicycle class examples. It is not in Greenfoot, so you probably did not review it. It is actually in the Java site. The home page of the tutorials is here There is an abundant supply of information linked to this page; however, the second sub-title on the left in red is 'Trails Covering the Basics', and the second item listed under that is 'Learning the Java Language'. Click on that and you will be on your way.
You need to login to post a reply.