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

2016/1/10

Mouse info

1
2
TentaBreak TentaBreak

2016/1/10

#
Hello, I'm just starting with Greenfoot and I'm trying to make an easy game, for starters. I have a problem, because I don't know how exactly should I define the Mouse actor? Basically, I have animals displayed on my world and their food ( random ). So for, example, I have a Monkey and a Banana, how should I enter write the code for this, so that the Monkey when it's clicked, will be selected and if Banana is clicked next, then the pair disappears. But if it's incorrect the previous tile isn't selected anymore? I tried making, and combining codes together, but in the end nothing worked. I don't have any valid codes to show, I just want to know in which direction should I look, or go? And also, if someone could come up with a code that would help a ton. Thank you for any kind of information!!
danpost danpost

2016/1/10

#
Place an instance field in your World subclass to hold any Monkey clicked on. So, if the value of the field is 'null', then you are waiting for a monkey click; and if the value of the field has a reference to a Monkey object, then you are waiting on a banana click. Set the field to null on any click when the field has a reference to a Monkey object.
TentaBreak TentaBreak

2016/1/11

#
Thank you for reply, uh.. so If I get this right, I must write this code in my World subclass which is called " TheGame ". And in there I must write the code under the Act command? That's where I would put it lol. I tried some codes but I don't really know. I tried to set transparency aswel, but it's not doing anything. public void act() { if (Greenfoot.mouseClicked("Monkey")) { //code } if ("Monkey".equals("Banana")) { } }
TentaBreak TentaBreak

2016/1/11

#
Ah, I was not supposed to post that! damn you " Tab " Button. Anyway, yeah the code above is nothing actually, but I was writing some things before I tried posting it and I accidentally post it. But I feel like I must insert Else section aswel, should I not? I don't really know how to work with the "Null" things either. But thank you Danpost for replying, I will lurk around some more later.
danpost danpost

2016/1/11

#
It would be along these lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
// instance field in world class
private Monkey monkeyClicked;
 
// in act method of world class
if (monkeyClicked == null)
{
    // if monkey clicked, set monkeyClicked to that monkey
}
else // if any click
{
    // if banana clicked, remove monkeyClicked and banana
    // set monkeyClicked to 'null'
}
TentaBreak TentaBreak

2016/1/16

#
Thank you for your post, I tried doing something around with your code, but I can't get it to work. I defined the monkey click, but it doesn't work. It keeps saying " cannot find method Monkey ". I did tried making private Monkey clicked to public, doesn't work. I tried to define it in the Actor class and it doesn't work either ( note: the Monkey is in world defined and called as M1). I'm doing something wrong and idk what. Also, the banana click. Normally when you go over, the mouse cursor changes, but it stays the same. It won't allow me to click it. I tried even defining the 'flippping' method (et new back and all.. ) and even to match Monkey and Banana. I did get no errors but the code didn't work. I'm complicating but I don't know where. I tried transparency method and it gave me so many errors I just canceled it lol. I do have every animal and its food defined seperately below Actor's class, but that shouldn't be a problem right? True, I am new to programming, but.. I feel stupid I can't get simple code to work ~ ugh
danpost danpost

2016/1/16

#
TentaBreak wrote...
It keeps saying " cannot find method Monkey ". I tried < blah, blah >. I tried < blah, blah >.
Why are you not posting the code itself? first, where you got the error message and second, where you tried this and that. Use code tags when posting code (see the link -- 'Posting code? read this!', below the reply box.
TentaBreak TentaBreak

2016/1/18

#
Right, I tried everything in world subclass called "Game", but every animal and food actors are seperated under Actor class, idk if I mentioned that before. I tried different code now, and the code below is the one I recently made. I don't wana do the tiles or flipping things anymore, I just messed my whole code up.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    private Monkey monkeyClicked;
    private Banana bananaClicked;
     
    public void Act()
    {
      {
 
     if ( monkeyClicked == null)
      {
         return;
      }
     if (bananaClicked == monkeyClicked)
     {
       removeObject(monkeyClicked);
       removeObject(bananaClicked);
     }
    else
    {
     }
     monkeyClicked = null;
     bananaClicked = null;
  }
}
So, Error now says : Incompatible types : Banana and Monkey. Well, I hope the code thing works now!
danpost danpost

2016/1/18

#
Lines 7, 11 and 12 of the summary I gave above requires you to put the appropriate code at those locations (the comments describe what code should go at those locations). Your incompatable types is happening because you are trying to compare a field that is to hold a Monkey object with a field that is to hold a Banana object. It is like comparing a hand to a foot. Unless you specifically call the 'Act' method, it will not ever execute. Greenfoot calls a method called 'act' (starting with a lowercase character).
TentaBreak TentaBreak

2016/1/19

#
Hm.. okay so, if I got this right.. " // if monkey clicked, set monkeyClicked to that monkey " there is an If sentence, but how do you set monkeyClicked to that monkey? Is that just this then?
1
2
3
4
5
6
7
8
    public void act()
     {
        if ( monkeyClicked == null)
         {
            Monkey monkeyClicked;
            return;
         }
}
Also at line 9, you stated " else //if any click ", how do I put that in the code? Am I supposed to make new variable for the click? And in the 12 line, monkeyClick is set to null or do I have to set it again? Also for the "if banana clicked, remove monkey and banana" I have to make new banana variable and make it public, right? But yeah, I was trying to make it like, if banana or monkey are clicked, they both dissapear when one of them is clicked (doesn't matter which is clicked first). This isn't even that hard, I just can't come around it. I don't know how to properly set the onclick method. I know I'm making a fool out of myself, but I really can't set it properly.
danpost danpost

2016/1/19

#
The only lines that in my summary above that need replaced with code lines are the ones I mentioned (lines 7, 11 and 12). All the other comments are describing code that is already there. Lines 1 and 4 where telling you where the following code should be placed. The comment on the 'else' line describes what clicks will be processed with the following block of code. I guess it does not matter at this point because you want to change things up a bit by not requiring the monkey be clicked first. As such, you would have (in summary -- replace comments starting with double slashes with required code):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** instance field in world class */
private Actor firstClicked;
 
/** in act method of world class */
if (Greenfoot.mouseClicked(null))
{
    Actor mouseActor = // get actor clicked on (if any)
    if // (actor was clicked on AND it was either a Monkey or Banana object)
    {
        if (firstClicked == null)
        {
            firstClicked = mouseActor;
        }
        else
        {
            if // (first clicked on object is not same type as second clicked on object)
            {
                // remove both clicked objects
            }
            firstClicked = null;
        }
    }
}
TentaBreak TentaBreak

2016/1/20

#
So, this is the code I have now. I added second click (banana). No errors, so it should work.. but it doesn't. Or am I still missing something? I have 10 animals and 10 food. So If I get just one code it'll be easier to connect the others.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private Actor firstClick, secondClick;
   public void act()
   {
      if (Greenfoot.mouseClicked(null))
      {
        Actor mouseActor = firstClick;
         if ( firstClick == null)
          {
             firstClick = monkeyClicked;
             return;
            }
         Actor mouseActor1 = secondClick;
         if ( secondClick == firstClick)
         {
             removeObject(firstClick);
             removeObject(secondClick);
            }
           else
           {
              if (firstClick != secondClicked)
              {
                addObject(firstClick, 671, 89);
                addObject(secondClick,216, 324);
              }
              firstClick = null;
              secondClick = null;
            }
        }
danpost danpost

2016/1/20

#
You do not need a field for a 'secondClick' actor. The variable 'mouseActor' will contain that actor and it is not required to hold its value for more than the current run of the method. There are obviously some things missing here. To get the actor that was clicked on, you need to get a MouseInfo object (see the Greenfoot class for the method to use) and call the 'getActor' method (see the MouseInfo class) on it. Then you need to use the 'instanceof' keyword to see if the actor, if one exists, is a Monkey or Banana instance. Line 20 needs to compare the classes of the objects, not the objects themselves.
TentaBreak TentaBreak

2016/1/20

#
Okay, thank you. So after lurking around I came to the code below. Funny thing is, it lets me click everything and it dissapears, except the banana. And yes I understand that in line 20 (prev post) but it won't let me type in "Monkey", or anything else, not even m1, that's how I defined and added money in the world. Because it didn't work at very beggining I made Monkey = monkeyClicked. And it works. It's.. weird, but okay . Maybe I didn't added pictures correctly? Uh.. This small project turned out for very troublesome for me and professor at school makes a lot less sense than anything on this page. Anyway, the code is this ( It's 6AM here, I didn't sleep at all and I erased the comments out but I think I didn't delete any other code):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void act()
 {
  if (Greenfoot.mouseClicked(null))
  {
      MouseInfo mouse = Greenfoot.getMouseInfo();
      if (mouse == null) return;
      Actor mouseActor = mouse.getActor();
      if (mouseActor == null || mouseActor instanceof Banana) return;
        removeObject(mouseActor);
     if ( firstClick == null)
      {
         firstClick = monkeyClicked;           
         return;
       }
       if (firstClick == secondClick)
       {
           removeObject(firstClick);
        }
       else
       {
            addObject(firstClick, 671, 89);
        }
        firstClick = null;
        secondClick = null;
    }
}
danpost danpost

2016/1/20

#
Slowly getting there. Line 6 is not needed as you are in a block of code where the condition (line 3) is that the mouse was clicked. There is always a MouseInfo object returned when the mouse is clicked. Let me give a little hint here -- the final code for the act method will not have a single 'return' statement within it. Line 8 is a good start -- but why would you want to return if the actor clicked on WAS a banana? It seems you messed up on the bracketing somehow -- line 9 is removing ANY Actor object clicked on. Line 12 should set 'firstClick' to 'mouseActor'. Lines 15 through 18 is supposed to be the if block that belongs within the else part and you are still comparing objects -- not the classes of those objects. Line 21 is strange in that I though you were to remove both clicked objects from the world at once and you are supposedly adding one back into the world here (however, I do not believe it is removed from the world to begin with). Finally, the end code should not have neither the 'secondClick' nor the 'monkeyClicked' fields in it.
There are more replies on the next page.
1
2