Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Detecting a mouse click on a specific area of the screen

Author
Message
Chaosmatt
14
Years of Service
User Offline
Joined: 3rd May 2012
Location:
Posted: 3rd May 2012 08:34
ok so I'm on an iPad and copying all the code would take days so I shall explain.

I am creating a point and click adventure game, I tried using collisions but now I'm having trouble doing it that way and feel a more stable way to do this would be to load a sprite and set the area the sprite covers as a clickable area

Is this possible?

Something like

If mouseclick()=1 and mousey()>50 and mousex()>50 then gosub _sublabel

I also need to be able to have another loop running for the player HUD while this is going on

Hopefully that helps explain.

Thanks in advance guys
New World Order
21
Years of Service
User Offline
Joined: 31st Oct 2004
Location:
Posted: 3rd May 2012 11:51 Edited at: 3rd May 2012 11:53
Hey Chaosmatt,

yepp, your approach sounds pretty good! Note, though, that by this method you can only have rectangular "buttons" (or "hotspots" in a point-and-click-adventure). I remember there being an awesome tutorial on here that covers a variety of different ways to create buttons, including "odd shaped" ones using sprites.

Your code also looks pretty good, but note that you also have to check for the right and lower bounds of the buttons!


If you already know about arrays, you can also set up an array to contain all the buttons, their x and y values and the subroutine / function to execute when clicked

If I understand you correctly, I don't think you need a second loop for the hud... You could just put everything in one big main game loop and it would work..


I hope this helps! If I find that buttons-tutorial, I'll post the link

edit: fixed "<, > "typo in the code didn't actually try it out, but it should work
Chaosmatt
14
Years of Service
User Offline
Joined: 3rd May 2012
Location:
Posted: 3rd May 2012 13:24
Thanks for the reply. I will give that a try although all of that looks very confusing to me as I am still very new to all this and get confused when I see all the indents and x's and y's lol

Could you explain the return function, I looked on dark basic but it doesn't explain it very well

Thanks for your time
Millenium7
21
Years of Service
User Offline
Joined: 13th Dec 2004
Location:
Posted: 3rd May 2012 13:27 Edited at: 3rd May 2012 13:28
best method is probably to use 'zones' from Matrix1Util's. It's essentially the same as (if >x1 & >y1 & <x2 & <y2 then) but a bit easier to use and less manual coding. You predefine coordinates (in 2d or 3d space) as a zone number, and then check what 'zone' a position is, i.e. the mouse position. If it's not within the space of a 'zone' then it'll return a 0, otherwise the zone number. From that you can take the appropriate action

if zone>0 then DoAction(scene,zone)
Chaosmatt
14
Years of Service
User Offline
Joined: 3rd May 2012
Location:
Posted: 3rd May 2012 13:32
Also how can I have a variable increase after a given time? and disable the left mouse button?

So my player will have turns, each click uses a turn and once the turns run out they can't click and will have to wait for the turns to replenish. Now I have coded for when it his zero they must wait 2 seconds and get 2 more however while they have turns they won't increase, I would like it to increase every 30 seconds even if they still have turns, is there any way to run things alongside each other?

Hope my question makes sense

Regards
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd May 2012 15:17 Edited at: 3rd May 2012 15:18
Or, setup an off screen bitmap. Color the bitmap with your zones in whatever configuration you want. A certain color means a certain zone. It can be any shape you want also. Use point() to detect the color on that bitmap, or convert it to a memblock and update the position in the memblock based on the mouse screen position.

Enjoy your day.
New World Order
21
Years of Service
User Offline
Joined: 31st Oct 2004
Location:
Posted: 3rd May 2012 18:21
Heh, first off, the indents aren't there to confuse you - quite the opposite, really It's good practice to indent the contents of a "nest" such as a do---loop or an if---endif. It makes it much easier to keep a structure in your code

What I was talking about the x's and y' is the following:
if you want to check if the mouse is in a certain area of the screen, you have to specify the TOP, LEFT, RIGHT and BOTTOM borders of this area. Your check of mousex()>50 and mousey()>50 only checks whether the mouse is at least 50 pixels into the right of the screen and 50 pixels towards the bottom. So you will get a positive result for this check, unless you keep your mouse to the very left or the very top of your screen. That's why you also need to check for the right and bottom border of the "box" you want to see if your mouse is in it. check the attached pic, I'm sure it will make sense.

Ah, I think I see what really confused you about the x's and the y's. in the section

I just defined the variables "leftx", "rightx" etc. The colon just means that there are more than one command in one line, you could also write each thing in its own line. But the idea of naming variables and giving them values is a very important one for your program to keep track of its data.. It just means that, whenever I use the word "leftx" later on in the code, the program checks, "what is leftx's value? -> ah, it's 40" and just calculates with the value 40. There are different types of variables, the most important ones being integers (whole numbers, 1 , 2 , 3 ...), floats (numbers with a decimal point) and strings (a group of letters such as "Hello World" or "PlayerName" .... You should really check the dbpro help files on variable types, I know it might seem like a lot to wrap your head around, but its really essential to programming (and satisfying to learn ).


The return function closes a subroutine. Since you used the "gosub" command in your code I assumed you knew about it, sorry
Basically, when you call a subroutine from inside your main loop, the program "jumps" to the part of the source code that makes up this subroutine and does whatever it's told in there. When it hits the "return" command, it returns / jumps back to the point in the main loop where the subroutine had been first called. I hope this kind of makes sense...

Finally, about your last question, the simplest method would be to put something like this into the main loop:

The timer just runs along with your program, measured in milliseconds. What you do in this bit of code is see if the timer is sufficiently far ahead of the "last_givepoints" variable, which is a kind of time-marker. Whenever you get points "points = points + 2", the "last_givepoints" variable is updated to the current timer() value. Thus it takes another 2000 milliseconds (2 sec) for the if condition to be fulfilled AGAIN, resulting in 2 more points and another "reset" of the timer.

To implement that the player can only click when he has a positive number of points, just add another IF condition around the button:
(using the code from above)


i hope this helps xD otherwise just shout
Chaosmatt
14
Years of Service
User Offline
Joined: 3rd May 2012
Location:
Posted: 3rd May 2012 19:47
Just thought I'd reply to say thanks for the help I haven't had a chance to try these things out but I will give them a go.

Really appreciate it I have been struggling to find the right commands.

Thanks again guys
Chaosmatt
14
Years of Service
User Offline
Joined: 3rd May 2012
Location:
Posted: 3rd May 2012 19:50
Could you explain how to set up a function as I can't seem to set one up and from what I have seen it would help shorten my coding quite alot, at some point I'll get the code off my computer and post it up from another comp.

Thanks again guys
Chaosmatt
14
Years of Service
User Offline
Joined: 3rd May 2012
Location:
Posted: 4th May 2012 09:15
Sorry for all the questions I'm learning new things all the time ATM

I have created a function but the variables are not updating when being displayed. The variables are updating but not showing it in the text so the turns are decreasing but the number of turns is not decreasing despite using the same code I used outside the function.

Any help?

Thanks guys
chafari
Valued Member
20
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 4th May 2012 16:10
Hi there.

@Chaosmatt

What I do is divide the screen to know where we are. We can see that with an example and it works without the lines...they are just to show where we are...



As you comment, you can use a variable to disable mouse buttons...here another example...I hope it helps someone.



Cheers.

I'm not a grumpy grandpa
Chaosmatt
14
Years of Service
User Offline
Joined: 3rd May 2012
Location:
Posted: 6th May 2012 22:52
Ok guys thanks for your help so far I have not had a chance to get to a comp to get the code up.

I can decrease turns I can create activity buttons I'm getting better at writing and controlling variables etc however with all the sub routines it makes things very confusing, is there any other way to achieve the same result without effectively having a load of little isolated loops as it is getting very hard to implement certain things into the code.

Thanks for the help guys
Millenium7
21
Years of Service
User Offline
Joined: 13th Dec 2004
Location:
Posted: 7th May 2012 08:44
have a look at my post above. Easiest method is to simply use matrix1util's and the zones commands
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 7th May 2012 14:17 Edited at: 7th May 2012 14:21
This is the easiest way. It's called DBBuilder. I don't know who made it. DOWNLOAD....


The problem is though, if you drop an item you will need to calculate your new position. But from the download you will most likely learn how to manually code the positions.

Login to post a reply

Server time is: 2026-07-08 00:07:16
Your offset time is: 2026-07-08 00:07:16