MouseHit()

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by merendo.

Hello Guys and Girls;

Question: Is there in PB also a command working like MouseHit() in BlitzBasic? When the mousebutton is pushed, the function should return the pushed-value only once.

Thanks, merendo

We always need to hear both sides of the story (by Phil Collins)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by merendo.

Can you help me?

Cu @ll, merendo
--
I've been hiding - What am I hiding from???
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by traumatic.

why don't you simply set a variable after the mouseclick has occured and check if the value is like you set it?

pseudocode:

Code: Select all

if mousehit and hitonce1
   ; this only gets executed once
   hitonce=1
endif
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by merendo.

Already tried. Doesn't work:

Procedure MouseHit(button)
Protected hitbefore

If MouseButton(button) And hitbefore = 0
ProcedureReturn 1
hitbefore = 1
EndIf

If MouseButton(button) = 0 : hitbefore = 0 : ProcedureReturn 0: EndIf

EndProcedure



Cu @ll, merendo
--
I've been hiding - What am I hiding from???
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

> Protected hitbefore

This makes hitbefore always be 0 when the Procedure is called, change it to:

Shared hitbefore
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by traumatic.

of course it does :wink:

Code: Select all

Procedure MouseHit(button)
  Shared hitbefore
  If MouseButton(button) And hitbefore = 0 
    hitbefore = 1
    ProcedureReturn 1
  EndIf
EndProcedure


; mainloop
Repeat
  ExamineMouse()

  If MouseHit(1) = 1
    Debug "mouse hit once!"
  EndIf
Forever
see what your problems were?

regards,
traumatic
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by traumatic.
Originally posted by freak
oh, i didn't see your reply...

merendo: you also left your procedure before actually setting hitbefore=1




--------
good programmers don't comment their code. it was hard to write - should be hard to read!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by merendo.

See this code: It doesn't work. The 1 is always displayed when hitting the mouse, as if it is mousebutton().

Code: Select all

InitMouse()
InitSprite()

Procedure MouseHit(button)
  Shared hitbefore

  If MouseButton(button) And hitbefore = 0
    ProcedureReturn 1
    hitbefore = 1
  EndIf
  
  If MouseButton(button) = 0 : hitbefore = 0 : ProcedureReturn 0: EndIf

EndProcedure

If OpenScreen(640,480,16,"test")

  Repeat
    ExamineMouse()
    
    ClearScreen(0,0,0)
  
    StartDrawing(ScreenOutput())
      Locate(MouseX(),MouseY())
      DrawText(Str(MouseHit(1)))
    StopDrawing()
    
    FlipBuffers()
      
  Until MouseButton(2)

EndIf
Cu @ll, merendo
--
I've been hiding - What am I hiding from???
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by traumatic.

as i already tried to explain:
you're leaving your procedure before actually setting 'hitbefore' to 1.

you wrote:

ProcedureReturn 1
hitbefore = 1

but of course it has to be:

hitbefore = 1
ProcedureReturn 1


see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by merendo.

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

Cu @ll, merendo
--
I've been hiding - What am I hiding from???
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by traumatic.

what is it, you don't understand?

as soon as ProcedureReturn gets executed your are LEAVING the procedure.


procedurereturn 1 ; now leaving the procedure
hitbefore=1 ; this line NEVER gets executed


still no clue? let me now :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by merendo.

Sorry, I didn't know about this. I always thought, ProcedureReturn is as if a normal command, but I didn't know, that it quits the procedure. Does that mean, that ALL code after the ProcedureReturn won't be executed?

Cu @ll, merendo
--
I've been hiding - What am I hiding from???
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Does that mean, that ALL code after the ProcedureReturn won't be executed?
As soon as the ProcedureReturn command is executed it exits the procedure, take a look at this:

Code: Select all

Procedure.l TestProc(foo.l)
  Shared bar.l
  If foo > 0
    bar = 0
    ProcedureReturn 1 ; Here the procedure exits immidiately, no line of code
                      ; in the procedure is executed after this!
    bar = 1           ; so this line is a total waste of typing effort
  Else
    bar = 1
    ProcedureReturn 2 ; See above, same goes here...
  EndIf
  ; so putting lines of code here will not be good as they will never be executed!
EndProcedure
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by traumatic.
Originally posted by merendo

Sorry, I didn't know about this.
no deal. we're all here to learn :)



--------
good programmers don't comment their code. it was hard to write - should be hard to read!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by merendo.

@Pupil: OK!!! Thanks, I guess I have to say: procedurereturn result$ and only change the vari result$ :wink:

@traumatic: You don't know how right you are. Especially me!!!
Good night @ll

Cu @ll, merendo
--
I've been hiding - What am I hiding from???
Post Reply