Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / How do I get image variable from number

Author
Message
Trisect Development
21
Years of Service
User Offline
Joined: 8th Sep 2002
Location: Denmark
Posted: 25th Apr 2012 23:09
I define a sprite like this.



Now if I in a loop prints the value for GetSpriteHit(x, y) where (x, y) is on my sprite, I get a number returned if I print it.

Is it possible to get the variable mySprite in return and not the number given to mySprite.ImageNumber?

Keep track of your milage and deductions
Get Driver Logbook
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 26th Apr 2012 00:09
It's not possible to return the variable name, but with a loop, yoou can compare the two.

For example, you can do:

If MySprite.ImageNumber = GetSpriteHit(x, y) Then Collision = 1

(For ease of reading, it should probably be SpriteNumber in your example, and not ImageNumber.)

"here is a an expression to remember - He who has the gold makes the rules."
Paypal disagreed!
Trisect Development
21
Years of Service
User Offline
Joined: 8th Sep 2002
Location: Denmark
Posted: 26th Apr 2012 00:27
My problem is that in my game I have many sprites and they overlap even if I have set some of them to invisible. So GetSpriteHit sometime give me another sprite than the one supposed to.

I have developed some sprite controller code that I will use in my next game. But for this game I have to stay with the other solution.

Keep track of your milage and deductions
Get Driver Logbook
Grumpy Jedi
14
Years of Service
User Offline
Joined: 16th Jan 2010
Location:
Posted: 26th Apr 2012 02:33 Edited at: 26th Apr 2012 03:48
if I understand correctly you're trying to test if one of many sprites on the screen has been hit at a certain x,y coordinate and then return the index of the sprite, but you want it to be returned as the global variable mySprite, and you want the return value to only be the sprite that is visible.

Depending on just how many sprites you're talking about, you could do it like this...

Use an array to hold the sprite indexes as you create them.

EDIT: Took image assignment out of the array loop as I doubt your using a seperate image for each sprite.

EDIT: fixed the name of the array in the dim statement, forgot the s on the end of the name.



Now since GetSpriteHit does not take visiblity into account, you would need to check for the hit in another way...

if they are physics sprites you could use a 1 pixel wide by 1 pixel high dummy sprite and GetPhysicsCollision.

However if the sprites are not physics sprites you would need to use a 1 pixel wide/high invisible regular sprite and GetSpriteCollision


For a physics based sprite you could create the dummy sprite and test in your loop like this.



For non physics based sprites you could do it like this.



That will cycle through your sprites until it finds the first one that has been hit at the specified x,y coordinates, that is also visible, and then assign its index to the global variable mySprite and exit the for loop.

Is that what you're looking for?

Good luck...
Trisect Development
21
Years of Service
User Offline
Joined: 8th Sep 2002
Location: Denmark
Posted: 26th Apr 2012 15:36
As I mentioned I have some new sprite handling code and that uses arrays and indexes.
But I'll look at your code Grumpy Jedi, maybe you have some smart features I did not think of.
So Thanks.

I was just curious to know if it was possible to find my sprite variable from the sprite number and I guess I can't.

Keep track of your milage and deductions
Get Driver Logbook
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 27th Apr 2012 01:26
Mobiius solution should work, for those pesky hidden collisions, just add this around the collision check.



Grumpy Jedi
14
Years of Service
User Offline
Joined: 16th Jan 2010
Location:
Posted: 27th Apr 2012 02:44 Edited at: 27th Apr 2012 02:47
Quote: "Mobiius solution should work, for those pesky hidden collisions, just add this around the collision check.

if getspritevisible(GetSpriteHit(x, y))=1
If MySprite.ImageNumber = GetSpriteHit(x, y) Then Collision = 1
endif"




I don't think that would work Vader.

GetSpriteHit(x,y) returns only the first sprite it finds by draw order whether visible or not.

So unless you know the sprite you're looking for is at a closer depth, or at the top of the draw queue, the visibility check on GetSpriteHit() may never equal 1.
DVader
20
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 27th Apr 2012 03:31
Probably not, looking at it, I think you are right, other sprites may interfere regardless of being hidden or not. I normally have a set range of sprites and loop through them for collision checks myself, or I sometimes use some of the physics collision commands. The sprite group commands seem pretty useful too, but I haven't played too much with them yet, beyond disabling collision with certain groups of sprites when using physics.

bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 27th Apr 2012 21:21
move the invisible sprites out of the screen area to location 0, -999 for example
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 27th Apr 2012 21:29
Create sprite groups and move invisible sprites into another group.

"here is a an expression to remember - He who has the gold makes the rules."
Paypal disagreed!
Trisect Development
21
Years of Service
User Offline
Joined: 8th Sep 2002
Location: Denmark
Posted: 28th Apr 2012 20:53
@bjadams

Thats what I'm doing in my new code.

Keep track of your milage and deductions
Get Driver Logbook

Login to post a reply

Server time is: 2024-05-04 19:56:53
Your offset time is: 2024-05-04 19:56:53