Author Topic: Functions in Script  (Read 2423 times)

eric22222

  • Fanatic
  • ***
  • Posts: 177
    • View Profile
    • Eric Online
    • Email
Functions in Script
« on: 2006-12-14, 11:08:09 AM »
Alright, I'm having more script problems. We have our player throwing a spear at an enemy. There's a collision between the two sprites, and I want to activate a function in script that will create a death animation sprite based on the enemy that was killed. Here's what I've got:

Code: [Select]
Dim SprDead

Sub Player_OnSpritesCollide(Name, ClsASprIdx, ClsBSprIdx, CollDefIdx)
   Dim SprA, SprB
   Set SprA = ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.Sprite(ClsASprIdx)
   Set SprB = ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.Sprite(ClsBSprIdx)
   If CollDefIdx = 1 Then
      If left(SprB.rDef.Name, 4) = "Slug" Then
         KillSprite (ClsBSprIdx)
      End If
   End If
End Sub

Function KillSprite (SprDead)
   Dim X, Y
   Dim NewSpr, Lyr
   X = SprDead.X
   Y = SprDead.Y
   Set Lyr = ProjectObj.GamePlayer.rmap.MapLayer("Main")
   Lyr.RemoveSprite SprDead
   If left(SprDead.rDef.Name,4) = "Slug" Then Set NewSpr = Lyr.pMap.SpriteDefs("Dead_Slug_" + MapNum).MakeInstance
   Lyr.AddSprite(NewSpr)
  NewSpr.X = X
  NewSpr.Y = Y
End Function

I get an error message saying "Object required: SprDead." From what I understand, it should have the value from where I pass it to the function. What's the deal here?

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2748
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Functions in Script
« Reply #1 on: 2006-12-14, 02:06:14 PM »
ClsBSprIdx is just a number indicating the index of the sprite within its layer.  If you want the actual sprite object, you need to pass SprB also, and use SprB when referring to SprB.rDef.Name, SprB.X and SprB.Y, and continue to use SprDead when calling Lyr.RemoveSprite.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Functions in Script
« Reply #2 on: 2006-12-15, 12:15:17 AM »
Also, you don't need to Dim variables that are passed into a function.
Edward Dassmesser

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2748
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Functions in Script
« Reply #3 on: 2006-12-15, 06:36:25 AM »
What variables did he Dim that were passed in?

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Functions in Script
« Reply #4 on: 2006-12-15, 09:03:26 AM »
I'm looking at Dim SprDead at the top and in the Function KillSprite (SprDead)
Edward Dassmesser

eric22222

  • Fanatic
  • ***
  • Posts: 177
    • View Profile
    • Eric Online
    • Email
Re: Functions in Script
« Reply #5 on: 2006-12-15, 04:17:33 PM »
Ok, I think I got everything. Had to look at some other script for the syntax on multiple parameters, but I got it.