18 Replies Latest reply: Apr 20, 2012 6:05 AM by Production Monkey RSS

    set spawn points for each sprite?

    elimann

      heres my dilema

       

      i currently have 5 sprites and 5 different set co ordinates for each one to spawn into with individual behaviour scripts, however i want each sprite to spawn into one of the set co ordinate without more than one of them spawning into the same point.

      so eventually each sprite will spawn into a random set co ordinate

       

      so something like

       

      pRandomStartingLoc = (200,100) (300,100) (400,100) so each sprite spawns into only one set of cordinates without more than one spawning into the same one

        • 1. Re: set spawn points for each sprite?
          Production Monkey Community Member

          You want something like the following:

           

          --  Random Set behavior

           

          property  pSetNum

          property  pMySet

           

          On GetPropertyDescriptionList me

            Props = [:]

            Props[#pSetNum] = [#default:#1, #format:#integer, #Range:[#min:1, #max:5], #comment:"Choose a set."]

           

            return Props

          end GetPropertyDescriptionList

           

           

          on beginSprite me

            Case pSetNum of

              1:  pMySet = [point(200,100), point(300,100), point(400,100)]

              2:  pMySet = [point(200,100), point(300,100), point(400,100)]

              3:  pMySet = [point(200,100), point(300,100), point(400,100)]

              4:  pMySet = [point(200,100), point(300,100), point(400,100)]

              5:  pMySet = [point(200,100), point(300,100), point(400,100)]

            end case

           

            put pMySet

          end beginSprite

          • 2. Re: set spawn points for each sprite?
            elimann Community Member

            would i use this code for each sprite?

             

            i tried the above code on my sprite but they didnt spawn at any of them points?

            • 3. Re: set spawn points for each sprite?
              Production Monkey Community Member

              Yes you would use one behavior for all five sprites.

               

              You need to modify the code I wrote and add it to your behavior. And, of course each "pMySet" property in the case statement would be set to a different list of points.

               

              The general idea is that if a set of sprites all have the same behavior but some of their properties need to be set to different values, then you use the " GetPropertyDescriptionList()" handler to set those differenct values.

              • 4. Re: set spawn points for each sprite?
                elimann Community Member

                it still didnt work, i put the code in a new behaviour as a test to see if they would spawn at different points but nothing is happening to my sprite

                • 5. Re: set spawn points for each sprite?
                  Production Monkey Community Member

                  The code I posted is Not complete. You need to write the "spawning" code. I only show how to pick a different set of points for each sprite.

                   

                  If your spawing code continues to not work, post your code so we can see what is not working.

                  • 6. Re: set spawn points for each sprite?
                    elimann Community Member

                    what kind of "spawning code" before my behaviour just assigned a certain co ordinate to each sprite

                     

                    eg

                     

                    property ch

                    on beginSprite me

                      ch = me.spriteNum

                     

                      start_x = 100

                      start_y = 284

                      sprite(ch).locH = start_x

                      sprite(ch).locV = start_y

                    • 7. Re: set spawn points for each sprite?
                      Production Monkey Community Member

                      I thought perhaps you were using some variation of the code Mister MUX wrote.

                       

                      Here's a more complete version:

                       

                      --  Random Set behavior

                       

                      property  pSetNum

                      property  pMySet

                      property  pSp

                      property  pStartLoc

                       

                       

                      On GetBehaviorDescription me

                        return "Sets sprite to a random location based off a predefined set."

                      end GetBehaviorDescription

                       

                       

                      On GetPropertyDescriptionList me

                        Props = [:]

                        Props[#pSetNum] = [#default:#1, #format:#integer, #Range:[#min:1, #max:5], #comment:"Choose a set."]

                        return Props

                      end GetPropertyDescriptionList

                       

                       

                      on beginSprite me

                        pSp = Sprite(me.spriteNum)

                       

                        -- get my location choice set

                        Case pSetNum of

                            -- *** Change these value to what you want *****

                          1:  pMySet = [point(200,100), point(300,100), point(400,100)] 

                          2:  pMySet = [point(100,200), point(500,105)]

                          3:  pMySet = [point(50,70), point(478,387)]

                          4:  pMySet = [point(400,500), point(623,245)]

                          5:  pMySet = [point(600,400), point(284,470)]

                        end case

                       

                        pSp.loc = pMySet[random(pMySet.count)]   -- set my location

                        pStartLoc = pSp.loc  -- store current location in case you want to return to it

                      end beginSprite

                       

                       

                      on goToStartLoc me

                        pSp.loc = pStartLoc

                      end

                      • 8. Re: set spawn points for each sprite?
                        elimann Community Member

                        thanks this worked for the spawning however they can still spawn into the same co ordinates?

                         

                        eg i tried with 2 sprites

                         

                         

                            1:  pMySet = [point(100,200), point(500,105)]

                         

                            2:  pMySet = [point(100,200), point(500,105)]

                         

                        however set 1 and set 2 sprites still sometimes spawn into the same co ordinate? i want only one sprite in one co ordinate at a time

                        • 9. Re: set spawn points for each sprite?
                          Production Monkey Community Member

                          I'm not sure what you want. Is this closer?

                           

                          --  Random Set behavior

                           

                          property  pPosNum

                          property  pSp

                          property  pStartLoc

                           

                           

                          On GetBehaviorDescription me

                            return "Sets sprite to a random location based off a predefined set."

                          end GetBehaviorDescription

                           

                           

                          On GetPropertyDescriptionList me

                            Props = [:]

                            Props[#pPosNum] = [#default:#1, #format:#integer, #Range:[#min:1, #max:5], #comment:"Choose a set."]

                            return Props

                          end GetPropertyDescriptionList

                           

                           

                          on beginSprite me

                            pSp = Sprite(me.spriteNum)

                            pSp.loc = [point(200,100), point(300,100), point(400,100), point(500,100), point(600,100)][pPosNum]   -- set my location

                            pStartLoc = pSp.loc  -- store current location in case you want to return to it

                          end beginSprite

                           

                           

                          on goToStartLoc me

                            pSp.loc = pStartLoc

                          end

                          • 10. Re: set spawn points for each sprite?
                            elimann Community Member

                            for example if i have 2 objects

                             

                            object 1 can spawn at (100,100) or (200,200)

                            object 2 can spawn at (100,100) or (200,200)

                             

                            if object 1 spawns at (100,100) then object 2 must spawn at (200,200) and vice versa

                            • 11. Re: set spawn points for each sprite?
                              Production Monkey Community Member

                              Okay fourth time may be the charm.

                               

                              This behavior contains a Single Locations list that is global to all sprites with this behavior attached. Each sprite picks a random loc from the list and then deletes that value from the list so that no other sprite can use it.

                               

                              Note: This uses some advanced Lingo, but you should only need to replace the point values in the list of point values.

                               

                               

                              --  Random Set behavior

                               

                              property  pSp   -- this sprite

                              property  pStartLoc  -- this sprites location at beginsprite

                               

                              -- global/static behavior property

                              property  curLocs  -- list of sprite locations used by ALL sprites with this behavior attached

                               

                               

                              on beginSprite me

                                -- define our quasi global locations list if it is not defined or is empty

                                if me.script.curLocs.voidP OR me.script.curLocs = [] then

                                  me.script.curLocs = [point(200,100), point(300,100), point(400,100), point(500,100), point(600,100)]  -- Define Sprite Locs Here

                                end if

                               

                                Locs = me.script.curLocs

                                pSp = Sprite(me.spriteNum)

                                pSp.loc = Locs[random(Locs.count)]   -- set my location to random point from list

                                Locs.deleteOne(pSp.loc)  -- remove this Loc from list so that no other sprite can use it.

                                pStartLoc = pSp.loc  -- store current location in case you want to return to it

                               

                                -- debug code. Check message window

                                put "Sprite " & me.spriteNum & " is at location " & pSp.loc

                              end beginSprite

                               

                               

                              on goToStartLoc me

                                pSp.loc = pStartLoc

                              end

                               

                               

                              on endSprite me

                                me.script.curLocs = void

                              end

                               

                               

                              on reset me

                                me.endSprite()

                                me.beginSprite()

                              end

                              • 12. Re: set spawn points for each sprite?
                                elimann Community Member

                                tried this with 2 objects using points [point(200,100), point(300,100)]

                                 

                                didnt work again, the objects were able to spawn at the same co ordinates

                                 

                                eg object 1 and object 2 both spawned at 200,100 when only one of them should have, then the other should have spawned at 300,100

                                 

                                 

                                EDIT IGNORE IT WORKED

                                • 13. Re: set spawn points for each sprite?
                                  elimann Community Member

                                  just to add, how would i restore the position of a given sprite in relation to this code?

                                   

                                  would i use  pStartLoc?

                                  • 14. Re: set spawn points for each sprite?
                                    Production Monkey Community Member

                                    pStartLoc holds the starting location point and you can use that to modify the code, or you can just call the goToStartLoc() handler. As in:

                                     

                                    sprite(5).goToStartLoc()

                                     

                                    sendAllSprites(#goToStartLoc)

                                     

                                     

                                    Delete the "reset()" handler above. It will not work correctly. I would delete it from the forum but the forum won't let me at the moment.

                                    • 15. Re: set spawn points for each sprite?
                                      elimann Community Member

                                      I used the code above "sprite(5)goToStartLoc()" but changed the 5 to ch so I can have a universal code for all the cast members I am using, I used it to try make them respawn but they all respawned back onto the random coordinate it was assigned to i.e all to (300,100). I want it so a member who was at (400,200) will return to that exact coordinate and another member for example at (600,400) with return to that exact coordinate.

                                       

                                      Any help will be appreciated.

                                      • 16. Re: set spawn points for each sprite?
                                        Production Monkey Community Member

                                        sendAllSprites(#goToStartLoc)

                                        • 17. Re: set spawn points for each sprite?
                                          elimann Community Member

                                          sendAllSprites(#goToStartLoc)

                                           

                                          this just sent all the sprites back to one random co ordinate, i want each sprite to return back to its each individual random cordinate that we used in the example code above

                                           

                                             eg using your code before

                                           

                                          me.script.curLocs = [point(200,100), point(300,100)]

                                           

                                           

                                          if sprite 1 spawns at (200,100), sprite 2 spawns at (300,100). im creating a dragging game so if i drag the sprite 1 to a wrong location i want sprite 1 to return back to (200,100), however as you know sprite 1 could also spawn at (300,100) next time. so basically i want whatever random co cordinate the sprite spawns at the start, for it to return to that same given co cordinate if a given action occurs

                                           

                                           


                                          • 18. Re: set spawn points for each sprite?
                                            Production Monkey Community Member

                                            The code I posted works exactly the way you want it to work. Each sprite returns to the random location that was picked on beginSprite. If it does not do that then you perhaps changed some code or have another behavior attached to the sprites that is causing your problem.