Author Topic: Mouse-moving sprite  (Read 1999 times)

eric22222

  • Fanatic
  • ***
  • Posts: 177
    • View Profile
    • Eric Online
    • Email
Mouse-moving sprite
« on: 2006-10-03, 08:26:04 AM »
Using the asteroid project as my template, I tried to make a sprite move with the mouse. Not working.

Here's what I've got:

Code: [Select]
Dim CursorSprite
Dim MouseX, MouseY

Sub FindCursor()
   Dim I
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      For I = 0 To .SpriteCount - 1
         If .Sprite(I).rDef.Name = "Cursor" Then
            Set CursorSprite = .Sprite(I)
            Exit Sub
         End If
      Next
   End With
End Sub

Sub Display_MouseMove(Button, Shift, X, Y)
   If IsEmpty(CursorSprite) Then
      FindCursor()
      Exit Sub
   End If
   With ProjectObj.GamePlayer
      CursorSprite.X = X + .MapScrollX - .rMap.ViewLeft
      CursorSprite.Y = Y + .MapScrollY - .rMap.ViewTop
   End With
   MouseX = X
   MouseY = Y
End Sub

Sub Player_OnAfterMoveSprites()
   If IsEmpty(CursorSprite) Then
      FindCursor()
      Exit Sub
   End If
   With ProjectObj.GamePlayer
      CursorSprite.X = MouseX + .MapScrollX - .rMap.ViewLeft
      CursorSprite.Y = MouseY + .MapScrollY - .rMap.ViewTop
   End With
End Sub

HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16

From what I can tell, CursorSprite is being set to the right sprite; it moves around a bit when the map scrolls, just like if you don't move the mouse in the asteroid project. When I run my project, it acts as though the mouse isn't moving. That is, it just hangs out in the upper-left corner of the screen. My guess is that I did something wrong in the Sub Display_MouseMove part.

So... any suggestions?

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Mouse-moving sprite
« Reply #1 on: 2006-10-03, 12:20:02 PM »
The line HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player" allows you to access events raised by the ProjectObj.GamePlayer object, such as Player_OnAfterMoveSprites() where Player is equal to whatever you put in between the quotes in the SinkObjectEvents line.  You need a similar line to access events raised by the display object:

Code: [Select]
HostObj.SinkObjectEvents CurrentDisplay, "Display"

This line can be placed near the line with the player.  This line is present in the Asteroid.vbs file, if you want to copy its location from there.
Edward Dassmesser

eric22222

  • Fanatic
  • ***
  • Posts: 177
    • View Profile
    • Eric Online
    • Email
Re: Mouse-moving sprite
« Reply #2 on: 2006-10-03, 01:01:54 PM »
Okay, I tried some of that split script stuff that was done in the asteroid script. Now it works. Hooray!
But, do I need to use any of that split stuff? I still don't think I need to, but that I somehow changed something when I used it...

I'm going to try and make some stuff happen when I click the mouse. You'll hear from me again if it doesn't work.

Thanks!

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2748
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Mouse-moving sprite
« Reply #3 on: 2006-10-03, 04:11:16 PM »
The #split stuff is necessary when you want to handle events from the display because:
1. You must set up all the events before calling ConnectEventsNow.
2. You must set up the event handlers for an object only after the object exists.
3. The display object only exists after Play is called.
4. Play must be called after ConnectEventsNow.

Sounds impossible, so I had to implement support for that "split" stuff to make it possible by having multiple scripts.  Under those circumstances, you can call play in the first script, and create and connect a new set of events in the second script.

eric22222

  • Fanatic
  • ***
  • Posts: 177
    • View Profile
    • Eric Online
    • Email
Re: Mouse-moving sprite
« Reply #4 on: 2006-10-03, 04:53:16 PM »
Well, I think I got it all working. This is all leading up to what I'm guessing will just be a demo project, though I may go through with a full game. Only time will tell, I guess. I won't tell you exactly what I'm doing here until I've got a fully-playable level done.