[SOLVED] Code problem, or event problem?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

[SOLVED] Code problem, or event problem?

Post by MaxV » Tue Oct 02, 2018 2:00 pm

Hello,
I'm trying to create a snake game in livecode, but I found a very strange bug.
First of all this is my code:

########CODE to copy and paste with your mouse#######
on arrowKey pKey
switch pkey
case "left"
changedir "-1,0"
break
case "right"
changedir "1,0"
break
case "up"
changedir "0,-1"
break
case "down"
changedir "0,1"
break
end switch
end arrowKey


on moveSnake
lock screen
put the number of graphics into tnum
repeat with i=1 to tnum
#movimento
put the loc of graphic i into tLoc
put the pdir of graphic i into tpdir
add item 1 of tpdir to item 1 of tLoc
add item 2 of tpdir to item 2 of tLoc
set the loc of graphic i to tLoc #new loc
#cambio direzione
if the short name of graphic i is not "head" then
put the pdirlist of graphic i into tpdirlist

if item 1 to 2 of tpdirList = the loc of graphic i then
put line 1 of tpdirList into tpdirList1
set the pdir of graphic i to (item 3 to 4 of tpdirList1)
set the pdirlist of graphic i to (line 2 to -1 of tpdirList)
end if
end if
end repeat
unlock screen
send movesnake to me in 0.02sec
end moveSnake

on changedir tDir

set the pdir of graphic "head" to tdir
put the number of graphics into tnum
repeat with i=1 to tnum
if the short name of graphic i = "head" then next repeat
put the pdirList of graphic i into temp
put the loc of graphic "head" & comma & tdir & comma & return after temp #loc,dir
set the pdirList of graphic i to temp
end repeat

end changedir
########END OF CODE generated by this livecode app: http://tinyurl.com/j8xf3xq ########
########Code tested with livecode 9.0.1########

The code works this way at the present, there is a graphic head, all the rest of the graphics are the body parts. When the user use the arrow keys, the snake change movement direction. This change is added to each graphic with custom properties.
Each graphics have these custom properties:
  • pdir: where to go (one pixel at time), it's like "-1,0"
  • pdirlist: when to change diection and where. It's a list like

    Code: Select all

    168,200,-1,0
    200,50,0,1
    300,50,1,0
When the body graphics reach their first point, their pdir changes that line is deleted.

Well, if the user play slow, all work. If the user play fast, I mean hit the arrow keys very close one after another, then the pdirlist lose the return chars, so sometime the list become:

Code: Select all

168,200,-1,0200,50,0,1
300,50,1,0
with horrible results :?
Do you have an idea of the problem causes?
Last edited by MaxV on Thu Dec 20, 2018 8:31 am, edited 1 time in total.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9579
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Code problem, or event problem?

Post by dunbarx » Tue Oct 02, 2018 4:43 pm

Hi,

I built much of your gadget, with about a dozen extra graphics apart from the one named "head"

I could not make the custom property fail to include the return no matter how fast I tried to press an arrow key. I even had the property build into a field so I could watch it grow.

It is what I would have expected, Livecode being faster than my finger, with a very short handler to run through.

Craig Newman

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Code problem, or event problem?

Post by MaxV » Wed Oct 03, 2018 10:41 pm

This video show the bug: http://www.maxvessi.net/temp/bug.mp4

Please can you try it?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Code problem, or event problem?

Post by capellan » Sun Oct 21, 2018 2:51 am

Hi All,

Today, Bogs posted a link to this forum thread.
I was trying to understand why this bug happens
but when I test the code, could not make it
work as show in MaxV video.

Where do you call the handler "moveSnake"
in the arrowkey and "changeDir" handlers?

Thanks in advance!

Al
LC9_MaxV_moveSnake_game.livecode.zip

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Code problem, or event problem?

Post by MaxV » Mon Oct 22, 2018 12:14 pm

I put the code in the card.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: [SOLVED] Code problem, or event problem?

Post by MaxV » Thu Dec 20, 2018 8:39 am

I found the problem and the solution.
If a graphic has a even number of pixel size (4x4), the loc is rounded (2,2 or 3,3 else), and some time is not rounded equally for all graphics, so along the movement path some graphic can't pass on the pixel on the other graphics and they can't activate the turn code and so they are lost.
EXAMPLE, 4x4 pixels graphic, WHICH PIXEL IS THE LOC?

Code: Select all

****
****
****
****
If a graphic has a even number of pixel size (5x5), the loc is precise:

EXAMPLE, 5x5 pixel graphic, WHICH IS THE LOC?

Code: Select all

*****
*****
**L**
*****
*****
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: [SOLVED] Code problem, or event problem?

Post by bogs » Thu Dec 20, 2018 11:13 am

That is pretty clever Max, I would never have caught it.
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”