Still not understanding 'sprite.value'

enonod

Well-Known Member
Licensed User
Longtime User
I have obviously not understood the obtaining of a sprite from an anonymous sprite.
I have...
B4X:
Sub gw_CollisionDestination
   Spr.New1
   Spr.value=gw.Sprite1
   If Spr.ID="cw" Then
The 'If' line gives the error that object is not set to an instance of an object.
Previously in this sub I was able to refer directly to the sprite because there was only one. Now I want to add a second, then the collision is caused by an anonymous sprite and I have to find out which, so I understand that I must treat the sprite as 'generic' until I find its ID, then I presume I could use the now known sprite direct.

[EDIT]
But why doesn't my code work?
And why must I declare Spr.New1 locally and not in App_Start (where it gives an error must use New)?
 
Last edited:

agraham

Expert
Licensed User
Longtime User
But why doesn't my code work?
I've no idea but the problem isn't in that code fragment.

And why must I declare Spr.New1 locally and not in App_Start (where it gives an error must use New)?
You should only need to New Spr once and you should be able to do it in App_start. You need to post more complete code so we can see the failure occur, not try to guess it from code fragments.
 

enonod

Well-Known Member
Licensed User
Longtime User
Lesson learnt, always post the full code. Unless like MI6 chief's wife, it's secret. :)
New Sprite is because I forgot to comment one out after constantly switching lines to test. Code attached.

All worked fine using only the cw sprite direct in the destination collision sub. Not sure how (I didn't prefix with gw.), this can be seen by commenting lines 32, 38, 39, 40, 46.
However, as it stands now...
Enabling line 32, disabling 38 causes the error requiring New.
Line 40 causes the current error.
I obviously don't fully understand something about the passing of the sprite to avoid gw.cw.ID I see the need but have somehow...?

A question out of interest. When the sprite goes around at a higher velocity, say 5, the sprite overruns the destination. I understand that it steps multiple pixels and may miss, so that a later pixel in the sprite 'arrives'. What I don't understand is why it only does it in this case, while travelling in directions 0 and 90 while 180, 270 are still perfect. Are you able to suggest?
 

Attachments

  • SpriteTesta.zip
    1.5 KB · Views: 132

agraham

Expert
Licensed User
Longtime User
Line 29 is wrong. You are calling the event Sub directly in App_Start before any collision has occurred so there is no sprite object in gw.Sprite1, which is only set when there is a collision, hence the error.

B4X:
28  'Set the first destination
29  [COLOR="Red"]gw_CollisionDestination[/COLOR]

You need this instead

28  'Set the first destination
29  cwTurn = 2
30  cw.DestinationX=cwWayPoints(cwTurn)
31  cw.DestinationY=cwWayPoints(cwTurn+1)

180, 270 are still perfect.
At a constant velocity steps to the right and down are matched by the same number of steps left and up so the sprite always return to its starting X and Y coordinates.
 

enonod

Well-Known Member
Licensed User
Longtime User
Oh yes, how stupid, and now, how obvious, when you point it out.
I was of course trying to save code, a thing I have an obsession about but usually it traps me as now.
Velocity... yes understood, thank you for both.
 

agraham

Expert
Licensed User
Longtime User
All worked fine using only the cw sprite direct in the destination collision sub. Not sure how (I didn't prefix with gw.)
Because you haven't reassigned its value to another Sprite or Newed it again so it still has the reference to the original Sprite added to the game window. Prefixing it with gw won't work as you can't access a Sprite added to the game window in that way. All those references to cw in the 'If Sptr.ID = "cw" Then' if block should really be to Spr.
 
Last edited:

enonod

Well-Known Member
Licensed User
Longtime User
Yes, thank you. I was still unsure of Spr.value in that I half believed I had to 'copy' it back to Sprite1 after changes, which would have been a pain. When I remembered that Spr was only a reference to Sprite1 and anything I do to Spr directly affects Sprite1 then all was well. I now do it properly and understand. It takes a little while. :)
All is going well, and I haven't thought of any extras.
 
Top