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

Re: Trying to explode a submarine using a sprite

Post by chris25 » Thu Nov 28, 2013 8:30 pm

Ok Gentlemen, please do not laugh, but it has taken me two and half days to write this collisions code alone. (you could do it in 10 minutes I think) :) But the learning process has been exceptional. And I know I will be able to transfer this learning over into the app side of things, but I am determined to finish this game despite having been tempted to throw it away once or twice. Anyway here is the collisions code that works finally and the submarine changes through the 7 images to a full out explosion after the mine bops it on the head. I have also enclosed a picture of just after the mine strikes and the sub changes.

Code: Select all

on collisions
   if 6 > gSubInterval then
      add 0.5 to gSubInterval  --determines speed of sequence of image change
   else
      put 0 into gSubInterval
      if intersect ( btn "SubExplode" , img "mine1" ) then
         if gSubExplode is 7 then
            put 1 into gSubExplode
         else
            add 1 to gSubExplode
         end if
         set the icon of btn "SubExplode" to the id of img ("blacksub" & gSubexplode & ".png" )
      end if
      --answer "oops"
      --exit to top
   end if
end collisions


So as you can see I have yet to learn how to:

1. Make the mine disappear from the script (not just hide/set visible to false since I do not want it to continue running an unnecessary script, since hidden, it will still be falling down ad finitum)
2. I need the sub to cycle through the 7 images when the intersect function begins and complete its 7 image cycle and then on the 7th image it will fall gently to the bottom. (this is easy except for the problem that when the intersect function begins, this cyclic event stops as soon as the mine passes onwards) I understand exactly why, But I have to learn how to fix this. If i make the mine disappear, and stop the script, I will still have to learn how to continue the cyclic event of the submarine exploding until completion, and then the damaged sub falls to the bottom of the ocean.

I hope I have written clearly enough and that no confusing points lurk anywhere.

Kind regards
chris

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:45 pm

We can't see from this how you are handling the mine dropping, so to stop it exploding the sub and still passing through and dropping to the bottom we need to see the code that makes it drop.
For the sub not exploding except while the mine is intersecting you could break out the explosion part into a separate handler, along the lines of

Code: Select all

      if intersect ( btn "SubExplode" , img "mine1" ) then
         makeItGoBoom
      end if
and in a separate handler

Code: Select all

      on makeItGoBoom
         put true into sDeadStatus 
         repeat with i = 1 to 7
            set the icon of btn "SubExplode" to the id of img ("blacksub" & i & ".png" )
         end repeat
      end makeItGoBoom
if sDeadStatus is a "flag" (script local variable - ie a variable that is accessible to all the handlers in the script) that you set to false at the beginning of the game then you can check for the status in the "updateSubPositionOrSpeed" handler (whatever you have named it). If sDeadStatus is true then you can stop it responding to keypresses and just drop it to the bottom, or else if it is false and still not dead then you can carry on reacting to the user control. That's one way of approaching things anyway - I'm not sure how it fits with the rest of your code, but hopefully you see the idea about setting a status flag and allowing the movement/action according to the state of the flag.

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 8:48 pm

Hallo just a quick response, I am only using one mine to keep things simple for the moment though I have 9 ready and waiting to code in. Anyway here is the code for the mine dropping.

Code: Select all

--set the loc gives illusion of more mines dropping
on DropMines
   set the top of img "mine1" to the top of img "mine1" + 1
   if the top of img "mine1"  > the bottom of card "surface" then
   end if
   --   set the top of img "mine2" to the top of img "mine2" + 1
   --   if the top of img "mine2"  > the bottom of card "surface" then
   --      set the loc of img "mine1" to 590,1
   --   end if
end DropMines

Code: Select all

global gSubExplode
global gSubInterval

local tGameRunning

on preOpenCard 
   set the acceleratedRendering of this stack to true
   set loc of button "SubExplode" to 26,214
   put 0 into gSubInterval
end preOpenCard

on startGame
   activateScreenUpdates
end startGame

on stopGame
   put false into tGameRunning 
   stopUpdatingTheScreen
end stopGame

on updateScreen 
   if tGameRunning is true then
      moveBox
      DropMines
      collisions
   end if 
   put true into tGameRunning
end updateScreen
EDIT: hallo sparkout. You said: so to stop it exploding the sub and still passing through and dropping to the bottom

This was where I started three days ago this was the problem an this is where I began,But this is not the plan or the problem now. the plan is that the mine explodes the the submarine upon intersection, and this works fine. Also the sub then cycles through its sprite of 7 images showing the explosion taking place. this works fine. EXCEPT that the sub cycles through only a part of the 7 images because when the mine passes onwards the cyclic event stops. It's the fact that the mine then needs to disappear and the sub must cycle through the 7 images to completion and then on that 7th image I can script the broken sub to fall to the bottom of the ocean.

regards
chris
Last edited by chris25 on Thu Nov 28, 2013 8:58 pm, edited 2 times in total.

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:51 pm

and how are you calling the DropMines handler? in a repeat loop? send in <time>?

and EDIT:

You would need to do something like setting a status flag for the mine and only call DropMines if it has not exploded.

eg in the "makeItGoBoom" handler, as well as cycling the sub explosing icons, you could put "boom" into sMine1
in the main loop, (on updateScreen) you would

Code: Select all

if sMine1 is not "boom" then
   DropMines
end if
This would need a bit of refinement to deal with multiple mines etc, but you see the idea? Status true = do something, Status false = do something else/nothing. It's a pretty common principle.
Last edited by SparkOut on Thu Nov 28, 2013 9:05 pm, edited 2 times in total.

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 9:00 pm

Think we cross-posted. See previous edits.

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 9:11 pm

ok thankyou sparkout.
regards
chris

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 9:18 pm

looking closer it might be better to test the status of the mine within the MineDrop handler, for instance

Code: Select all

on DropMines
   if sMine1 is true then
      set the top of img "mine1" to the top of img "mine1" + 1

      if the top of img "mine1"  > the bottom of card "surface" then
         -- the mine has reached the bottom so we can get rid of it
         put false into sMine1
         hide image "mine1"
         set the top of image "mine1" to -30
         -- as well as hiding, we can move it to off the top of the screen so it won't be tested in any intersects and
         -- will be ready to drop again if you reinitialise it later
      end if

   end if
end DropMines
this way would make it easier to deal with multiple mines in future. We would also set the sMine1 flag and hide it in the makeItGoBoom part of the script, to stop it dropping any further once it's exploded on the sub.

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 10:40 pm

Hallo again, I presume when you use the term "flag" you mean consistently "script local" variable? Thankyou for the extra information. Will work on this.
Kind regards
chris
Last edited by chris25 on Thu Nov 28, 2013 11:09 pm, edited 3 times in total.

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 11:02 pm

Yes, a flag is just a variable (or container or property) that is set to indicate a status. Mostly a Boolean state (true or false) but as I intimated you could put the state "boom" in the mine flag and check for that. Your tGameRunning variable is a flag, you test whether the game is running or not by checking the status of this flag and setting it to false when you want the game to stop.

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 11:09 pm

EDIT:
reading through this post and what has been said I think maybe I am missing something important about the way in which a variable can be declared? for example, can a variable be declared/be given meaning at the point of:

on preopenstack
put false into tVariable
end preopenstack

on somehandler
if tVariable is true then
put 1 into box
end if
end somehandler

has the variable's status at this moment been described to the LC program? Where by virtue of the if and then context the variable has been given meaning? Yes I know I display ignorance here but there is a subtle difference between what I have learned so far and seeing this sort of thing. Or am I wrong completely?

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

Re: Trying to explode a submarine using a sprite

Post by SparkOut » Fri Nov 29, 2013 1:27 am

A variable is "given meaning" when you reference it, either to declare its scope or to put something in it. A variable has "scope" according to where you mention its declaration in the script. A "script" is the single page you write lines in the code editor, if you edit the script of a button you have a page which by default shows an empty on mouseUp handler. You can add other handlers to that same script page. This is a script that is local to the button.
The card will have a script that is local to the card, the stack will have its own script etc. Each script can contain different handlers and functions.
If you put in a script "global gVariable" and repeat that on other scripts, then all the scripts will be able to access the value of gVariable. If the card script added 1 to the gVariable and the button script answered the value of gVariable then (as long both scripts declared them as global) on clicking the button the new value (with the 1 added) will be answered.
A "script local" variable is a variable which you declare outside any handlers and functions in a script, but "local" to the script page itself. This makes it accessible to all the handlers and functions on the same script page. You could:

Code: Select all

local sVariable

on checkDeath
  if sVariable is true then
    answer "Game over"
  end if
end checkDeath

on moveMe
  if sVariable is false then
   -- the death flag is still not set so do some moving
  end if
end moveMe
(not proper code samples but you get the idea). The variable sVariable is "declared" at the top of this script - which gives it a script local scope. This means that both the checkDeath and the moveMe handlers can both obtain the value in sVariable. However, another handler in another script could not access its value - if you tried, it would assume that there is another variable with the same name in its own script (there is nothing to stop you having the same variable names in different scripts but it can get confusing if you don't remember which value is meant to be in which scope).

If you had the same script as the script local example above but did not put the "local sVariable" declaration at the top outside the handlers, then each of the moveMe and checkDeath handlers would check their own "local" variable which could have independent values - as mentioned just now. The checkDeath sVariable value would not necessarily match the moveMe sVariable value.

Is that any clearer? If not I'll try to find a way to explain a better way.

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

Re: Trying to explode a submarine using a sprite

Post by chris25 » Fri Nov 29, 2013 11:45 am

Hallo Sparkout, Ok I need to make clearer what I am not understanding at this moment. Thankyou.

on customHandler
put 10 into sVariableX
blah blah blah
put sVariableX + tVariableZ into Fld "xcz"
end customHandler

In this example above the sVariableX has been given a parameter/a meaning/some programmable functionality. I have worked already with these, although I confess your last paragraph alerted me to the fact that I was using the t and the s prefixes the wrong way around - won't happen again. Anyway the above example is straightforward, sVariable declared outside of handler, no need to declare tVariable anywhere except within handler of course - all fully understood now.

However
on customHandler
put false into sVariableX
blah blah blah
if sVariableX is true then
put sVariable + tVariableZ into Fld "xcz"
if sVariable is false then
put 5 + tVaribale into fld "mnb"
end customHandler

However, however, however, In this example above although the sVariableX has been given a clear parameter (binary 0 or 1), and it also has a meaning, I see absolutely no programmable functionality by virtue of the fact that I am asking: Well what exactly is True or false (1 or 0), the sVariable is true or false, but it has to have another piece of information declaring exactly what this sVariableX has to assess as true or false. This is where I am a little lost on how to use a variable within this true/false context. Sorry - I am dumb and slow here for whatever reason I have no idea, when you answer I will see why I missed whatever it is that I missed. Unless one says the following somewhere in that same handler:

On custom handler/command
If cats are blue then
put true into sVariableX
if not then
put false into sVariableX
end custom handler/command

so here we have a piece of scripting that now gives functionality to the sVariableX, ?? and now we simply call the handler or the command to put this variable into action I simply don't see otherwise how I can understand this. And I know you will have something, but this is as far as I can stretch my logic.

Kind regards
chris

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

Re: Trying to explode a submarine using a sprite

Post by SparkOut » Fri Nov 29, 2013 5:23 pm

Hi Chris, I think you're getting there and you'll soon have it clear.

You're quite right that it would not make sense to have

Code: Select all

on customHandler
  put false into sVariableX
  --- blah 
  if sVariableX is true then
     -- do something
  else
     -- do something else
  end if
end customHandler
because if you put the value of false into it at the beginning of the handler, then the if/then test will only have one possiblity (false) and therefore be redundant. The point of the "flag" variable is that it can have its status set by different events. Then, most likely in completely different handler (which still has access to the variable to check), you can check for the flag's status and decide what to do accordingly. Let's relate this to your mines dropping and trying to avoid being blown up in the submarine.
You can have a script local variable, for argument's sake: sSubAlive, which you initialise to true when the game is started. When you check for the intersect of the sub and mine, if the collision is detected then you can set the flag: put false into sSubAlive. Then in your main loop for dealing with movements according to user control, you can check for the status of the sSubAlive and if it is true then deal with the movement, else it's dead so you can have the sub sink to the bottom and not bother responding to the user control.
LiveCode is a "typeless" language - which means that you can have any sort of data in a container, we don't have to restrict it by declaration to only boolean, or only integer, or only text, etc. We could just as easily say: put "alive" into sSubAlive at the beginning of the game and put "dead" into sSubAlive when a collision is detected. It's still a variable which is assigned the job of keeping track of the status, so that we can check what to do - so, yep, a "flag". You do already understand the notion, you have a flag tGameRunning which you set the status of true at the beginning of the game. (You might not need to be forcing it back to true every updateScreen iteration but...) In the stopGame handler you set the tGameRunning flag to false. Then next time the updateScreen handler happens, it checks the status of this flag and only if tGameRunning is true then you deal with the moveBox, DropMines and collisions handlers.
You've got it already!

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

Re: Trying to explode a submarine using a sprite

Post by chris25 » Fri Nov 29, 2013 6:02 pm

This is getting to complex. I can not create a variable for this, it has to be a separate command called by collisions. And it has to be defined by a custom handler.

So On SubSink
define the move with an automated Move command (thank goodness not invented) and that's it.
end subsink

then I include subsink within the collisions handler this is the only method that seems starighforward.

I just can not see how to make this a variable with true or false; The sGameRunning is set to true of false easy because it is defined within another handler ie On stopgame put false into sGamerunning, so declaring it as true is easy, Sorry.

kind regards
chris

EDIT:
This is what I did....


on SubSink
move btn "subExplode" from 26,214 to 376,516 in 16 secs without waiting
End SubSink

SCRIPT HERE
end if
set the icon of btn "SubExplode" to the id of img ("blacksub" & gSubexplode & ".png" )
end if
SubSink
end if
end collisions

This above works fine but...

But this does not work: I have decared the variable and put false in it inside the main handler etc:

if sSubSink is true then
move btn "subExplode" from 26,214 to 376,516 in 16 secs without waiting
end if
MORE SCRIPT HERE
set the icon of btn "SubExplode" to the id of img ("blacksub" & gSubexplode & ".png" )
end if
put true into sSubSink
end if
end collisions

So I really do not understand so sorry.

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

Re: Trying to explode a submarine using a sprite

Post by SparkOut » Fri Nov 29, 2013 6:29 pm

You have this code already, I'm going to add a bit and comment

Code: Select all

global gSubExplode
global gSubInterval

local tGameRunning, sSubAlive
-- add a script local variable for the flag to tell whether the sub is alive

on preOpenCard 
   set the acceleratedRendering of this stack to true
   set loc of button "SubExplode" to 26,214
   put 0 into gSubInterval
end preOpenCard

on startGame
   activateScreenUpdates
   -- I presume that one of the things the activateScreenUpdates handler does
   -- is to set the tGameRunning flag to true 
   -- (otherwise it will be that it initialises the updateScreen loop which sets it anyway)

  -- now, let's set the sSubAlive flag (or reset on new game start)
  put true into sSubAlive
end startGame

on stopGame
   put false into tGameRunning 
   stopUpdatingTheScreen
end stopGame

on updateScreen 
   if tGameRunning is true then
      -- we check the tGameRunning flag and only do the game updates if it's running
      moveBox
      DropMines
      collisions
   end if 
   put true into tGameRunning
   -- why do you reset the game running flag to true here?
end updateScreen

on moveBox
  -- you would be doing something like

  -- check if the sub is still alive
  if sSubAlive is true then
    -- it's still alive so deal with the movement
    -- I'm not sure what code you have for that
  else
    -- the sub has been exploded so
    set the top of button "SubExplode" to the top of button "SubExplode" + 1
  end if
  -- and let's see if the sub has hit the bottom
  if the top of button "SubExplode" > the bottom of this card then
    -- let's end the game
    stopGame
    -- which sets the tGameRunning flag to false and stops the loop
  end if

end moveBox

on collisions
   if 6 > gSubInterval then
      add 0.5 to gSubInterval  --determines speed of sequence of image change
   else
      put 0 into gSubInterval
      if intersect ( btn "SubExplode" , img "mine1" ) then
         -- the sub has been hit so let's break out the next action to another handler
         makeItGoBoom
      end if
   end if
end collisions

on makeItGoBoom
   -- the sub has been hit, so let's mark it with the status flag
   put false into sSubAlive
   
   -- do our fancy explosion 
   repeat with i = 1 to 7
            set the icon of btn "SubExplode" to the id of img ("blacksub" & i & ".png" )
   end repeat
end makeItGoBoom
There are probably a few bits missing or need to be adjusted to suit your game but you should hopefully see the flags are initialised somewhere, and checked for action according to the status. An event (such as being hit) will change the status of the flag. Hope this is clearer now.

Post Reply