Trying to explode a submarine using a sprite

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

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Trying to explode a submarine using a sprite

Post by chris25 » Wed Nov 27, 2013 8:12 pm

Local tSubExplode has been declared on this card: If I put 0 into this local variable, upon collision the mine just passes the sub.

on preOpenStack
put 0 into tSubExplode
end preOpenStack

If I then add a 1 to the above, then upon collision the sub just disappears. I am trying to make the sub (in a button) go through a routine of 5 sequential .png's already on that card and hidden.

on subExplosion
--Loop through the sub explosion images, once the last one has been reached.
if tSubExplode is 0 then
--Set the new sequentially numbered subExplosion image.
set the icon of button "BlackSub" to "ExplodingSub_" & tSubExplode & "_.png"
end if
end Subexplosion

on collisions
if intersect (btn "blackSub" , img "mine1") then
subExplosion
--set the icon of button "BlackSub" to "exStage1.png" (works fine)
--answer "stop game"
--exit to top
end if
end collisions

Would be nice to have some little guidance here - thankyou
kind regards
chris

dhurtt
Posts: 42
Joined: Sat Nov 09, 2013 7:37 pm

Re: Trying to explode a submarine using a sprite

Post by dhurtt » Wed Nov 27, 2013 8:55 pm

The key problem seems to be:

Code: Select all

if tSubExplode is 0 then
  set the icon of button "BlackSub" to "ExplodingSub_" & tSubExplode & "_.png" 
end if
If the variable tSubExplode = 0 then it will show icon "ExplodingSub_0_.png". If it is any other value then it will not reset the icon. Perhaps you mean "> 0"?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Trying to explode a submarine using a sprite

Post by Klaus » Wed Nov 27, 2013 9:12 pm

Hi friends,

ALWAYS use parens when concatenating object names:
...
set the icon of button "BlackSub" to ("ExplodingSub_" & tSubExplode & "_.png")
...
But to be honest, I don't get your problem at all 8)
When do you want to show your animation?
Waht condition needs to be met?


Best

Klaus

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Trying to explode a submarine using a sprite

Post by chris25 » Wed Nov 27, 2013 9:26 pm

Hallo, I have been working through the variable with breakpoints, and watching how even when I set it to 1 it adds 1 and still does nothing nothing at all. Just a disappearing sub all the time. :)

Yes Klaus I have read fourthworlds strategy and consistency for livecode scripting. I am trying to conform, as best as I can.

I want to show the animated effect on intersect, (mine hits sub), I have 5 images each one showing a submarine blowing up (png's that I made in photoshop).

When mine hits sub, the 5 image sequence replaces the submarine image (that is contained in a button). All then cease to exist.

I can easily set a btn icon to an image, I can make a gif animation appear (but the latter is not good for the LC game engine - understandably), But then I thought that by using scripting to achieve this effect my very simple easy game (that is now complexed itself by my urge for creativity even with self imposed strict limits) using LC to do the job has to be more advantageous for speed and fluency, and it gives me more skills.

Hope this explains it.

since asking the question the my new code in the custom handler is now:

Code: Select all

on subExplosion
      --Loop through the sub explosion images, once the last one has been reached.
   if tSubExplode is 2 then
      put 1 into tSubExplode
   else 
   add 1 to tSubExplode
   end if
      --Set the new sequentially numbered subExplosion image.
   set the icon of button "BlackSub" to ("ExplodingSub_" & tSubExplode & "_.png") -- please note addition of parenthesis :D 
end Subexplosion
But still not getting anywhere

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Trying to explode a submarine using a sprite

Post by Klaus » Wed Nov 27, 2013 9:32 pm

Loop = repeat, not?

Is that what you are looking for:

Code: Select all

...
repeat with i = 1 to 5
  set the icon of button "BlackSub" to ("ExplodingSub_" & i & "_.png")
  wait 100 millisecs with messages
end repeat
...
?

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Trying to explode a submarine using a sprite

Post by chris25 » Wed Nov 27, 2013 9:55 pm

Ok logically yes this should have been better actually. But still, the sub just disappears. Every time I check the button properties inspector, and it consistently re-sets the id 1009 (icon ID of sub image), it sets this every time to a 0. I really do not know why it would keep doing this. This would explain WHY the sub disappears, the icon id is being removed. But NOT being replaced with the .png ID's
regards
chris

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Trying to explode a submarine using a sprite

Post by Klaus » Wed Nov 27, 2013 10:22 pm

Hi Chris,

I never used image NAMES as an icon, so maybe you should try this with the actual image IDs?
Just a guess...


Best

Klaus

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Trying to explode a submarine using a sprite

Post by chris25 » Wed Nov 27, 2013 10:49 pm

I can not use ID numbers because there are five images. This would mean that the code here: set the icon of button "BlackSub" to ("ExplodingSub_" & i & "_.png") the 'ExplodingSub' would have to contain 5 ID numbers. Instead each of the images is named exactly the same but with the number 1 number 2 etc after the name. How can I use ID numbers in this?

splash21
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 369
Joined: Sun Dec 19, 2010 1:10 am
Contact:

Re: Trying to explode a submarine using a sprite

Post by splash21 » Wed Nov 27, 2013 11:59 pm

Try;

Code: Select all

 set the icon of button "BlackSub" to the id of image ("ExplodingSub_" & i & "_.png")
LiveCode Development & Training : http://splash21.com

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Trying to explode a submarine using a sprite

Post by Klaus » Thu Nov 28, 2013 1:42 pm

Hi Chris,
chris25 wrote:I can not use ID numbers because there are five images.
oh, come on! See below... 8)
chris25 wrote:This would mean that the code here: set the icon of button "BlackSub" to ("ExplodingSub_" & i & "_.png") the 'ExplodingSub' would have to contain 5 ID numbers. Instead each of the images is named exactly the same but with the number 1 number 2 etc after the name.
Hm, then you might need to use another code!

Make sure your 5 images have subsequent IDs. Like in this EXAMPLE:
1004 -> ExplodingSub_1_.png
1005 -> ExplodingSub_2_.png
1006 -> ExplodingSub_3_.png
1007 -> ExplodingSub_4_.png
1008 -> ExplodingSub_5_.png

Now translate the IDs to image names:
...
## We use ADDITION (to the first ID) to "compute" the correct ID:
repeat with i = 0 to 4
set the icon of button "BlackSub" to (1104 + i)
end repeat
...
chris25 wrote:How can I use ID numbers in this?
Check the ID of your image(s) and set the ICON of your button to that number.


Best

Klaus

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Trying to explode a submarine using a sprite

Post by chris25 » Thu Nov 28, 2013 4:57 pm

Pleased to say that I have it working, I got rid of underscores which I should not have used, I added a second if to the second line, and took Dhurtt's advice and used ID instead of names and of course used the variable name instead of 'i', though I know that you said this Klaus; and Klaus, I have just read your response. I meant that I could not used 5 ID's within this code as it stood. Klaus I will be trying out your suggestion as well, so much appreciated, as variety will help me learn, thankyou.

Code: Select all

on collisions
   if intersect ( btn "BlackSub" , img "mine1" ) then
      if gSubExplode is 5 then
         put 1 into gSubExplode
      else
         add 1 to gSubExplode
      end if
      set the icon of btn "BlackSub" to the id of img ("exStage" & gSubexplode & ".png" )
      --answer "oops"
      --exit to top
   end if
end collisions
I need to put a delay in between each successive image change. I thought of a Variable called tSubInterval whereby the variable would contain timed delay and then include this inside a repeat statement. I will try it, but it also would be nice to have another suggestion. Only asking for hints.

Kind regards
chris

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Trying to explode a submarine using a sprite

Post by Klaus » Thu Nov 28, 2013 5:23 pm

If you like more "food for thought", check my answer here:
http://forums.runrev.com/phpBB2/viewtop ... 552#p90445
8)

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Trying to explode a submarine using a sprite

Post by chris25 » Thu Nov 28, 2013 5:45 pm

Hi Klaus, I had no idea that you had replied to this it was so long ago. But I will have to take my time later on to go through this if you don't mind. I have a more pressing issue. Could you point me to any resources that talk about the different ways to prevent messages from being delivered within a handler. In other words, LC is running unseen routines in the background, for example, you hide an object and it is gone, but if that object is moving then valuable processing time is being used up and the object is still actually running in the background. Or you have 2 routines in one handler, the first one runs and is finnished and the second one starts, but the first one is still in effect taking up valuable processing time - I know that I am not using correct language to ask the question, but I am beginning to think a little deeper into processes and it is hard to know exactly what questions I should be asking.

kind regards
chris

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Trying to explode a submarine using a sprite

Post by Klaus » Thu Nov 28, 2013 5:56 pm

Hi Chris,

not sure what you mean? Could you create a script in "pseudo-code" that may give me a hint?


Best

Klaus

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Trying to explode a submarine using a sprite

Post by SparkOut » Thu Nov 28, 2013 8:08 pm

I don't know, but I'm guessing the problem is something like a "hidden" submarine that has already "exploded" is still responding to cursor keypresses and moving an invisible (or visible ghost) submarine which shouldn't be there any more?
Probably you are using something along the lines of "send updateSubPositionOrSpeed to me in some milliseconds" within the handler "updateSubPositionOrSpeed". You could set a "flag" (a variable with an "on/off" state) and when you initialise the game you set the flag to true. Then in the updateSubPositionOrSpeed handler you

Code: Select all

if sFlag is true then
   send updateSubPositionOrSpeed to me in 50 milliseconds
end if
When the sub explodes you set sFlag to false and so when the updateSubPositionOrSpeed handler happens next it does not send the next update and the ghost sub stops moving/responding. sFlag would be a script local variable that can be accessed by both the updateSubPositionOrSpeed and the explodeSubHere handlers.
At least it might be something along those lines. We'd need to see a bit more script to tell if this is appropriate to your situation, but I hope you get the idea here.

Post Reply