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

2019/4/24

Give points when cursor clicks on object.

rycode rycode

2019/4/24

#
Hi, I am working on my game and was wondering how to give points when an object is clicked by the mouse/cursor.
Nosson1459 Nosson1459

2019/4/24

#
use the mouseClicked(java.lang.Object obj) method in the greenfoot class, like this:
Greenfoot.mouseClicked(this); //this is if you put it in the object that's to be clicked on
Greenfoot.mouseClicked(Object obj); //this is if you put it elsewhere (such as in the world class). over here my class is named object but that's arbitrary.
danpost danpost

2019/4/24

#
Nosson1459 wrote...
use the mouseClicked(java.lang.Object obj) method in the greenfoot class, like this: << Code Omitted >>
Line 1 returns a boolean value and is equivalent to:
true; // or false;
which does nothing by itself (actually, both lines). But, by using it as a condition to score, you put it in an if statement:
if (Greenfoot.mouseClicked(this)) { << increment score here >> }
rycode rycode

2019/4/25

#
Thanks for commenting both of you (danpost I see you on every discussion possible btw and great games) , but, would I need anything else for this to work properly or just this. I have not stated score anywhere yet and this is a fresh scenario. So, I was wondering if you guys could help me state this and score together. Thanks (I am sort of new to this, but I have stated score a few times)
danpost danpost

2019/4/25

#
rycode wrote...
would I need anything else for this to work properly or just this. I have not stated score anywhere yet and this is a fresh scenario. So, I was wondering if you guys could help me state this and score together.
What I gave is basically it. It might take a little more doing to actually display the score, however.
rycode rycode

2019/4/25

#
if(Greenfoot.mouseClicked(this)) {1}
^^ Seen as not a statement
if(Greenfoot.mouseClicked(this)) {<<1>>}
^^ Illegal expression What I am thinking is that I have not stated score anywhere, which is not allowing me to do this OR I am missing something very big as I have had no experience with this.
danpost danpost

2019/4/25

#
You need to increment your score there. Since I do not know what you are using for your score or where your score field is, I cannot say exactly what code you would use there.
rycode rycode

2019/4/25

#
I just was about to make an edit, sorry
if (Greenfoot.mouseClicked(this))
        {
            score++;
        }
like this?
danpost danpost

2019/4/25

#
rycode wrote...
I just was about to make an edit, sorry << Code Omitted >> like this?
It could be as simple as that if your score field were in that class. But, it could have been more complicated, like:
((MyWorld)getWorld()).getScore().add(1);
if your score was elsewhere.
rycode rycode

2019/4/25

#
Ok, thanks. I was looking through all of your methods and demos and I saw one game kind of with the same score idea. https://www.greenfoot.org/scenarios/9571 Is this how simple your code was?
rycode rycode

2019/4/25

#
So, I think the score might be working, but I need to show it on a counter. Would it be possible for you to help me on that aswell. Thanks (Sorry for asking so many questions)
danpost danpost

2019/4/25

#
rycode wrote...
So, I think the score might be working, but I need to show it on a counter.
Maybe you should take a look at my Value Display Tutorial scenario.
You need to login to post a reply.