Page 1 of 1

Create unlimited objects that have their own behaviors

Posted: Sat Dec 05, 2015 5:06 pm
by problème
Hello

i would want to know how create a number of object unlimited. Each object move independently one of the others. I do this but when i press a key for the second time. I have a rectangle in the middle of my screen besides the graphic oval "balle".
I want to do when i press on a key , a "ball" be created and move.

Code: Select all

on keyDown k
  if k is space then
           Fire
  end if
end keyDown

command Fire
   create graphic "ball" 
   set the style of graphic "ball" to "oval"
   set the width of graphic "ball"  to 28; set the height of graphic "ball" to 12
   set the opaque of graphic "ball"  to true
   set the backgroundColor of graphic "ball" to "red" 
   --set the location of graphic "ball" to ...
   send "MoveBall" && "ball"
end Fire

command MoveBall graphicObject
   set the location of graphic graphicObject to (the item 1 location of graphic  graphicObject + 1), (the item 2 location of graphic  graphicObject)
   send "MoveBall graphicObject" to me in 2 milliseconds
end MoveBall

Re: Create unlimited objects that have their own behaviors

Posted: Sun Dec 06, 2015 6:21 pm
by Klaus
Bonsoir problème,

not sure what might cause this rectangle, but here some general hints:
1. ... in 2 millisecs
Means you fire 500 "Moveball" commands per second, noone will notice a 500 FPS animation!
And I'm not really sure that Livecode will handle this sufficiently for more than a couple of "balls" 8)

2. I hope you have an "emergency" button that will stop these pending messages if neccessary! :D

3. Some syntax problems:
...
## set the location of graphic graphicObject to (the item 1 location of graphic graphicObject + 1), (the item 2 location of graphic graphicObject)
...
You can use abbreviations for most LC object type and property names!
...
set the loc of grc graphicObject to (item 1 OF THE location of grc graphicObject + 1), (item 2 OF THE loc of grc graphicObject)
...
Maybe just modifying the loc itself will be a little faster.
Since we only modify the first item, we do not need to query this property a second time!
...
put the loc of grc graphicObject into tLoc
add 1 to item 1 of tLoc
set the loc of grc graphicObject to tLoc
...

4. Setting the neccessary properties directly in "the templategraphic" might even add a little more speed.

Code: Select all

command Fire
   set the style of the templategraphic to "oval"
   set the width of the templategraphic to 28
   set the height of the templategraphic to 12
   set the opaque of the templategraphic  to true
   set the backgroundColor of the templategraphic "ball" to "red" 
   set the name of the templategraphic to "ball"
   --set the loc of the templategraphic to ...

   ## All preparations successfull, now we can:
   create graphic

   ##send "MoveBall" && "ball"
   ## No need to send a command if it is in the same script or script hierarchy!
   ## Just execute it:
   MoveBall "ball"

   ## Always good style:
   reset the templategraphic
end Fire
5. Sorry, no quick and brilliant idea for your numbering and animation problem. :(

You will need to give all your graphics different names, since LC cannot differ objects with the same name!
In that case it will the address the object with this name in the lowest layer!


Best

Klaus

Re: Create unlimited objects that have their own behaviors

Posted: Sun Dec 06, 2015 6:32 pm
by Klaus
5. Sorry, no quick and brilliant idea for your numbering and animation problem. :(
Maybe you can manage a local variable that counts all new objects?

Code: Select all

local tCounter
## Maybe set it to 1 "on opencard"?
...
command fire
   if tCounter = empty then
      put 1 into tCounter
   else
     add 1 to tCounter
  end if
 ...
  set the name of the templategraphic to ("ball" & tCounter)
...
You get the picture :D


Best

Klaus

Re: Create unlimited objects that have their own behaviors

Posted: Wed Dec 09, 2015 10:45 am
by atmosk
Just a thought regarding the rectangle.
When you create a new grc, it will use the templateGraphic which by default is a rectangle (afaik) and it becomes a persistent phenomena when you're creating an excessive amount of graphics and that heaviness makes your script cough blood.
It will look like a single rectangle even though it's every graphic you're creating for .01(?) seconds before the style change.

So to fix that,

Code: Select all

create invisible grc "thingaling"
set the style of grc "thingaling" to "oval"
set the visible of grc "thingaling" to true
..and it might be a good idea to just set the style of the templateGraphic once so you don't need to set the properties for each graphic individually.

Re: Create unlimited objects that have their own behaviors

Posted: Thu Dec 10, 2015 3:24 pm
by problème
Hello
I do this with your suggestions. I have a problem, context : there is a object graphic "balle1" that created and move, when i create a new object graphic "balle2" , "balle1" blocks until "balle2" to be create.

Code: Select all

global numBalle, uneFoisTire

on keyDown touche
      if touche is "shoot" then 
         --put touche into tirerIsPressed
         tireNormal
      end if

command tireNormal
   add 1 to uneFoisTire
   if (uneFoisTire <= 1) then
      send "creationDuProjectile" to me in 500 milliseconds
   end if
end tireNormal

command creationDuProjectile
   set the style of the templategraphic to "oval"
   set the width of the templategraphic to 28; set the height of the templategraphic to 12
   set the opaque of the templategraphic  to true
   set the backgroundColor of the templategraphic to "red"
   set the name of the templategraphic to "balle"
   set the location of the templategraphic to ... 
   putID
   create graphic ("balle"& numBalle)
   put 0 into uneFoisTire
   send "MoveBall" && ("balle"& numBalle) to me in 150 milliseconds
end creationDuProjectile

command putID
   if numBalle = empty then
      put 1 into numBalle
   else
     add 1 to numBalle
  end if
end putID

command MoveBall graphicObject
   if  the right of the grc graphicObject >= the width of this stack then
      delete grc graphicObject
   else 
      set the loc of grc graphicObject to (item 1 OF THE location of grc graphicObject + 1), (item 2 OF THE loc of grc graphicObject)
      send "MoveBall graphicObject" to me in 2 milliseconds
   end if 
end MoveBall

Re: Create unlimited objects that have their own behaviors

Posted: Sat Dec 12, 2015 3:32 pm
by Klaus
Hi problème,

not sure what your exact problem is, but:

Code: Select all

global numBalle, uneFoisTire

on keyDown touche
      if touche is "shoot" then 
         --put touche into tirerIsPressed
         tireNormal
      end if

command tireNormal
...
Missing: END KEYDOWN

And the parameter that comes with KEYDOWN is of course the pressed key on the keyboard and so -> touche will NEVER be "shoot" or whatever!


Best

Klaus