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

2018/4/13

issues with Greenfoot.mouseClicked

knuckels5739 knuckels5739

2018/4/13

#
// i want to insert b2 with with b1 but i will error
if (Greenfoot.mouseClicked(b1)) {
        getWorld().removeObject(this);
    }
how i want it to work:
if (Greenfoot.mouseClicked( b1 ,  b2 )) {
        getWorld().removeObject(this);
    }
is it possible for the two objects to get clicked on and do the command. for example : i click on b1 and nothing happens but when i also click b2 then both of them will delete. could there be a workaround for this?
danpost danpost

2018/4/13

#
You will need to keep a reference to the first object that was clicked and then act when the second one is clicked. Or, better yet, use a byte field and give each of the actors a value (1 and 2, in this case) so that the sum of 3 will set off the action. For example:
// with field
private byte clickValue;

// action
if (Greenfoot.mouseClicked(b1)) clickValue |= 1; // clickValue ORed with 1 (Note:  1 OR 1 = 1)
if (Greenfoot.mouseClicked(b2)) clickValue |= 2;
if (clickValue == 3) // 1 | 2 = 3  (1 OR 2 = 3)
{
    getWorld().removeObject(b1);
    getWorld().removeObject(b2);
}
knuckels5739 knuckels5739

2018/4/15

#
how do i label the object as b1 instead of using "this" because puting b1 in (Greenfoot.mouseClicked(b1)) would give error
danpost danpost

2018/4/16

#
knuckels5739 wrote...
how do i label the object as b1 instead of using "this" because puting b1 in (Greenfoot.mouseClicked(b1)) would give error
Insufficient context given to answer. (what class the given code is in; what b1 and b2 refer to; what error was given; ...)
You need to login to post a reply.