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 / Sprite positioning not as expected

Author
Message
mindsclay
12
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 11th May 2016 10:39
I am trying to create a level meter type of display. The sprite (a rectangle) height is created randomly but I want the base of the sprite to remain at the same X,Y location. So I thought simple math would work, however I am seeing both ends of the sprite "move" as I am wanting the bottom to stay put. It seemed like the sprite's Y position should be the base location minus the height of the sprite. That is not working for some reason.

sprX and sprY is the location that I want the bottom of the sprite to be. sprH is the random height change.
If I want the base at 300 with the sprite height of say 50, then the sprite Y position should be 250. Correct? Because 250 plus 50 is 300. But that is not what happens!

But I am experiencing something like the height of the sprite is moving about a center location and not either end. I cannot see why my code would do this. Any insights?

Here is the code that runs in a loop:
home.wavecable.com/~mindsclay
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 11th May 2016 12:49
SetSpritePositionByOffset
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 11th May 2016 19:26
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
mindsclay
12
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 13th May 2016 05:18 Edited at: 13th May 2016 05:28
Mobiius: SetSpritePositionByOffset is doing the same thing as SetSpritePosition

CJB: I tried your snippet and I get no image at all.

The sprite is staying in position by "its center" not its top/left corner as the documentation states that it should. I tried using SetSpriteOffset to 0,0 but that made no difference.
Perhaps the math in my method is wrong??
home.wavecable.com/~mindsclay
mindsclay
12
Years of Service
User Offline
Joined: 1st May 2012
Location: Rocklin, CA, USA
Posted: 13th May 2016 05:59
I resolved my issue using SetSpriteScale. It is not exactly what I wanted, but I can make it do.
home.wavecable.com/~mindsclay
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 13th May 2016 07:05
You can setSpriteOffset() to the bottom of the sprite. Then the maths becomes simple because any scaling or resizing happens from the bottom of the sprite as the fixed point.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 13th May 2016 09:18 Edited at: 13th May 2016 09:19
Sorry... You need to set the virtual resolution for my code (it was just a snippet)... Try this:



But Batvink's solution is really nice! I like it.
V2 T1 (Mostly)
Phone Tap!
Uzmadesign

Login to post a reply

Server time is: 2024-11-25 08:51:30
Your offset time is: 2024-11-25 08:51:30