Page 1 of 2

Code Bugs

Posted: Wed Dec 25, 2013 3:54 pm
by itay1023
Hi All,
I am building know an app (Game) for the iOS Platform.

At a specific place in my game, if i drag a button, it needs to check if it was intersected with another objects.
There is a bug that during the collision check, it seems like the game detects a collision, and the score comes down or up also the life.
Here is the code:

Code: Select all

local LocVar
global _life

local tMessageID

global _startTheGame
global _score
global blackVar

on mouseDown
     if _startTheGame =false then
    exit mousedown
  end if

  put the loc of me into LocVar

if _startTheGame is true
then
  grab me
end if
  theButtonVar
end mouseDown

on mouseUp
  set the loc of me to LocVar

  cancel tMessageID
end mouseUp

on theButtonVar
  if intersect (btn "theButton" of card "gameCardNew", button "blueBubble" of card "gameCardNew", "pixels") then

   popSound
    hide button "blueBubble"
    set the loc of btn "blueBubble" to 581,269
    add 10 to _score
    put _score into fld "score" of this card
    hide btn "blueBubble"
  end if

  theButtonVar2
end theButtonVar

on theButtonVar2
  if intersect (btn "theButton" of card "gameCardNew", button "redBubble" of card "gameCardNew", "20") then
    add -1 to _life
    put _life into fld "life"
    hide button "redBubble"
    set the loc of btn "redBubble" to 581,269
    SavedRedBubbleActions
  end if

theButtonVar3
end theButtonVar2

on theButtonVar3
  if intersect (btn "theButton" of card "gameCardNew", button "goldBubble" of card "gameCardNew", "pixels") then
    add 100 to _score
    put _score into fld "score"
    hide btn "goldBubble" of card "gameCardNew"
    set the loc of btn "goldBubble" to 581,269
  end if
  theButtonVar4
end theButtonVar3

on theButtonVar4
  if intersect (btn "theButton" of card "gameCardNew", button "blackBubble" of card "gameCardNew", "pixels") then
    add -50 to _score
    hide btn "blackBubble"
    set the loc of btn "blackBubble" to 581,269
    put _score into fld "score"
  end if
  theButtonVar5
end theButtonVar4

on theButtonVar5
  if intersect (btn "theButton" of card "gameCardNew", btn "heartBubble" of card "gameCardNew", "pixels") then
    add 1 to _life
    put _life into fld "life" of card "gameCardNew"
    hide btn "heartBubble" of card "gameCardNew"
    set the loc of btn "heartBubble" to 581,269
  end if

  send "theButtonVar" to me in 100 millisecs

  put the result into  tMessageID 
end theButtonVar5 

Does someone can help me please?


Best regards,
Itay :)

Re: Code Bugs

Posted: Sun Dec 29, 2013 3:01 pm
by Mark
Hi Itay,

I think that grab is blocking. It means that your script will not continue to run until you release the mouse/finger. You maybe want to use

Code: Select all

local lMouseClicked

on mouseDown
  put true into lMouseClicked
end mouseDown

on mouseMove
  if lMouseClicked is true then
    -- do something
  end if
end mouseMove

on mouseUp
  put false into lMouseClicked
end mouseUp
You might also use the touch commands. Search the docs for "touch".

Kind regards,

Mark

Re: Code Bugs

Posted: Mon Dec 30, 2013 3:39 pm
by itay1023
It still doesn't work well.

Re: Code Bugs

Posted: Tue Dec 31, 2013 1:40 am
by Mark
Hi,

"It doesn't work" doesn't mean anything to a programmer. What happens and what do you expect or want to happen instead?

Kind regards,

Mark

Re: Code Bugs

Posted: Tue Dec 31, 2013 1:50 pm
by itay1023
See the code i wrote at the first message.
The button with that code, doesn't work well.
It detects a collision without a reason...

Re: Code Bugs

Posted: Tue Dec 31, 2013 2:59 pm
by Mark
Hi,

Have you actually tried using the mouseDown, mouseMove and mouseUp messages? Does this still detect collision without reason?

Kind regards,

Mark

Re: Code Bugs

Posted: Tue Dec 31, 2013 4:02 pm
by itay1023
Yes, i tried those commands and it still doesn't work well.

Re: Code Bugs

Posted: Tue Dec 31, 2013 4:14 pm
by Mark
Why don't you post your new script or provide some more information?

Mark

Re: Code Bugs

Posted: Tue Dec 31, 2013 4:34 pm
by itay1023
Here is my new script: :-)

Code: Select all

local LocVar
global _life
local lMouseClicked

local tMessageID

global _startTheGame
global _score
global blackVar



on mouseDown
   put true into lMouseClicked
     if _startTheGame =false then
          exit mousedown
   else
      grab me
  end if

put the loc of me into LocVar


  theButtonVar

end mouseDown


on mouseMove
   if lMouseClicked is true then
      theButtonVar
      end if
end mouseMove

on mouseUp
   put false into lMouseClicked
  set the loc of me to LocVar

  cancel tMessageID
end mouseUp

on theButtonVar
  if intersect (btn "theButton" of card "gameCardNew", button "blueBubble" of card "gameCardNew", "opaque pixels") then

   popSound
    hide button "blueBubble" ## with visual effect dissolve fast
    set the loc of btn "blueBubble" to -100,-100
    add 10 to _score
    put _score into fld "score" of this card
    hide btn "blueBubble"
  end if


  theButtonVar2
end theButtonVar

on theButtonVar2
  if intersect (btn "theButton" of card "gameCardNew", button "redBubble" of card "gameCardNew", "opaque pixels") then
       popSound
   add -1 to _life
       put _life into fld "life"
    hide button "redBubble"
    set the loc of btn "redBubble" to 100,-100
    SavedRedBubbleActions
  end if


theButtonVar3
end theButtonVar2

on theButtonVar3
  if intersect (btn "theButton" of card "gameCardNew", button "goldBubble" of card "gameCardNew", "opaque pixels") then
   popSound    
   add 100 to _score
    put _score into fld "score"
    hide btn "goldBubble" of card "gameCardNew"
    set the loc of btn "goldBubble" to 100,-100
  end if
  theButtonVar4
end theButtonVar3

on theButtonVar4
  if intersect (btn "theButton" of card "gameCardNew", button "blackBubble" of card "gameCardNew", "opaque pixels") then
   popSound   
    add -50 to _score
    hide btn "blackBubble"
    set the loc of btn "blackBubble" to 100,-100
    put _score into fld "score"
  end if
  theButtonVar5
end theButtonVar4

on theButtonVar5
   if intersect (btn "theButton" of card "gameCardNew", btn "redBubble2" of card "gameCardNew", "opaque pixels") then
   popSound    
   add -1 to _life
    put _life into fld "life" of card "gameCardNew"
    hide btn "redBubble2" of card "gameCardNew"
    set the loc of btn "redBubble2" to 100,-100
end if
theButtonVar6
end theButtonVar5

on theButtonVar6
  if intersect (btn "theButton" of card "gameCardNew", btn "heartBubble" of card "gameCardNew", "opaque pixels") then
   popSound    
   add 1 to _life
    put _life into fld "life" of card "gameCardNew"
    hide btn "heartBubble" of card "gameCardNew"
    set the loc of btn "heartBubble" to 100,-100
  end if
  

  send "theButtonVar" to me in 100 millisecs


  put the result into  tMessageID 
end theButtonVar6 


Re: Code Bugs

Posted: Tue Dec 31, 2013 4:55 pm
by Mark
Hi,

You have to remove the "grab me" command from your script. That's the point of using mouseMove instead. The mouseMove handler checks if the mouse was clicked and then can set the location of controls and check for intersections.

On iOS, there is a problem with mouseDown and mouseStillDown messages, the mouse function, and some other mouse-related features. Additionally, the "grab" command is blocking and won't work correctly with interset. Therefore, you need to forget your old script and use the mouseDown-mouseMove-mouseUp approach.

Kind regards,

Mark

Re: Code Bugs

Posted: Tue Dec 31, 2013 5:22 pm
by itay1023
Can you please write me a new code??

Re: Code Bugs

Posted: Tue Dec 31, 2013 5:28 pm
by Mark
Hi,

I'll give you an example. Use it to modify your code.

Code: Select all

local lMouseIsDown
on mouseDown
  put true into lMouseIsDown
end mouseDown

 on mouseMove
  if lMouseIsDown is true then
    put the mouseLoc into myLoc // you'll get weird things if you don't use a variable here
    set the loc of me to myLoc
    if intersect(the long id of me,the long id of "Some Other Control","pixels") then
      beep // or do whatever you want
    end if
  end if
end mouseMove

on mouseUp
  put fals einto lMouseIsDown
end mouseUp
This will work on iOS and Android.

Kind regards,

Mark

Re: Code Bugs

Posted: Tue Dec 31, 2013 5:43 pm
by itay1023
First of all, thank you very very much!
Second thing, it improved my game but not completely.
It still detect wrong collisions.
Here's the new code:

Code: Select all

local LocVar
global _life
local lMouseClicked

## Store the ID of the last SEND in a variable, so you can CANCEL it on "mouseup"!
## No need to check when the mouseis '"UP", right?
local tMessageID

global _startTheGame
global _score
global blackVar
local myLoc


on mouseDown
   put true into lMouseClicked
     if _startTheGame =false then
          exit mousedown
  end if

  ## Since this is ONE part of your complete "Collision detection" routine it should be called immaediately!
  ## No need to send it here.
end mouseDown


on mouseMove
   if lMouseClicked is true then
      put the mouseLoc into myLoc
      set the loc of me to myLoc
      theButtonVar
      end if
end mouseMove

on mouseUp
   put false into lMouseClicked
     set the loc of me to myLoc

  ## Now cancel the last SEND!
  cancel tMessageID
end mouseUp

on theButtonVar
  if intersect (btn "theButton" of card "gameCardNew", button "blueBubble" of card "gameCardNew", "opaque pixels") then

      ## A visual effect will also slow down things!
      ## NOTHING will happen during the effect!
      ## So maybe you wnat ot comment this out?
   popSound
    hide button "blueBubble" ## with visual effect dissolve fast
    set the loc of btn "blueBubble" to -100,-100
    add 10 to _score
    put _score into fld "score" of this card
    hide btn "blueBubble"
  end if

  ## See above...
  theButtonVar2
end theButtonVar

on theButtonVar2
  if intersect (btn "theButton" of card "gameCardNew", button "redBubble" of card "gameCardNew", "opaque pixels") then
       popSound
   add -1 to _life
       put _life into fld "life"
    hide button "redBubble"
    set the loc of btn "redBubble" to 100,-100
    SavedRedBubbleActions
  end if

  ## See above...
theButtonVar3
end theButtonVar2

on theButtonVar3
  if intersect (btn "theButton" of card "gameCardNew", button "goldBubble" of card "gameCardNew", "opaque pixels") then
   popSound    
   add 100 to _score
    put _score into fld "score"
    hide btn "goldBubble" of card "gameCardNew"
    set the loc of btn "goldBubble" to 100,-100
  end if
  theButtonVar4
end theButtonVar3

on theButtonVar4
  if intersect (btn "theButton" of card "gameCardNew", button "blackBubble" of card "gameCardNew", "opaque pixels") then
   popSound   
    add -50 to _score
    hide btn "blackBubble"
    set the loc of btn "blackBubble" to 100,-100
    put _score into fld "score"
  end if
  theButtonVar5
end theButtonVar4

on theButtonVar5
   if intersect (btn "theButton" of card "gameCardNew", btn "redBubble2" of card "gameCardNew", "opaque pixels") then
   popSound    
   add -1 to _life
    put _life into fld "life" of card "gameCardNew"
    hide btn "redBubble2" of card "gameCardNew"
    set the loc of btn "redBubble2" to 100,-100
end if
theButtonVar6
end theButtonVar5

on theButtonVar6
  if intersect (btn "theButton" of card "gameCardNew", btn "heartBubble" of card "gameCardNew", "opaque pixels") then
   popSound    
   add 1 to _life
    put _life into fld "life" of card "gameCardNew"
    hide btn "heartBubble" of card "gameCardNew"
    set the loc of btn "heartBubble" to 100,-100
  end if
  
  ## NOW send your routines in a short interval:
  ## hte message you want ot send in QUOTES!
  send "theButtonVar" to me in 100 millisecs

## STORE messages ID:
  put the result into  tMessageID 
end theButtonVar6 


Re: Code Bugs

Posted: Tue Dec 31, 2013 5:55 pm
by Mark
Hi

Why exactly is the collision wrong? I am working on a similar project and it detects the collisions correctly.

Kind regards,

Mark

Re: Code Bugs

Posted: Tue Dec 31, 2013 8:31 pm
by itay1023
The button doesn't return to it's first location (myLoc variable).