Posted: 5th Oct 2011 16:14
It seems straightforward, but I'm having problems understanding the concept based on what I'm seeing in my game.

I have a sprite with a size of 38x28. I set my sprite offset with

SetSpriteOffSet(sprite, 19, 14)


But when I want to get the current sprite position, using both GetSpriteXByOffSet and GetSpriteYByOffSet, the sprite seems off where it should be. Comparing the values returned above with the regular GetSriteX, the difference between the latter and the byOffset coordinates is about 5, instead of the usual 19.

That said, it seems my sprite offset is set a little to the left instead of exactly the middle.

I found the scale could affect the values to pass to the SetSpriteOffset, but I'm not setting the scale anywhere in my code, and even forcing the scale to 1 doesn't change a thing.

Any help will be appreciated.

Thanks
Posted: 5th Oct 2011 16:28
Are you using Virtual Resolution or Aspect Ratio?
Posted: 5th Oct 2011 17:47
I'm using virtual resolution but I actually don't know why - Is aspect ratio better for that case?

My Virtual resolution is set to 768x1024 (iPad portrait) but my setup.agc dimensions are set to 768x900 (so I can see on my laptop monitor as well).
Posted: 5th Oct 2011 17:54
If you were using a percentage based display, it would make a difference, but with a virtual resolution, it should not make a difference.

We could help more if we could see your code, or at least some example code that shows the problem.

Here is some code that shows the ByOffset working correctly...

+ Code Snippet
SetVirtualResolution(800, 600)
SetClearColor(125, 125, 125)

backSpr = CreateSprite(0)
SetSpriteSize(backSpr, 400, 300)
SetSpriteColor(backSpr, 100, 100, 100, 255)
SetSpriteDepth(backSpr, 20)
backSpr = CloneSprite(backSpr)
SetSpritePosition(backSpr, 400, 300)

text1 = CreateText("no offset, sprite x:200, y:150")
SetTextSize(text1, 15)
SetTextAlignment(text1, 1)
SetTextPosition(text1, 200, 20)
sprite1 = CreateSprite(0)
SetSpriteSize(sprite1, 150, 150)
SetSpritePosition(sprite1, 200, 150)

text2 = CreateText("75/75 offset, x:600, y:150")
SetTextSize(text2, 15)
SetTextAlignment(text2, 1)
SetTextPosition(text2, 600, 20)
sprite2 = CloneSprite(sprite1)
SetSpriteOffset(sprite2, 75, 75)
SetSpritePosition(sprite2, 600, 150)

text3 = CreateText("75/75 offset, x:200, y:450 BY OFFSET")
SetTextSize(text3, 15)
SetTextAlignment(text3, 1)
SetTextPosition(text3, 200, 320)
sprite3 = CloneSprite(sprite1)
SetSpriteOffset(sprite3, 75, 75)
SetSpritePositionByOffset(sprite3, 200, 450)


do
    Sync()
loop


Posted: 5th Oct 2011 18:33
Hi,

thanks for the answers so far.

This is the code where I create my sprite:

+ Code Snippet
// -----------------------------------------------------------
// Load ball object
// -----------------------------------------------------------
Function loadBall()
    ball.sprite = CreateSprite (ball.image)

    SetSpriteVisible(ball.sprite, FALSE)
    ball.x# = SCREEN_WIDTH/2
    ball.y# = SCREEN_HEIGHT/2 + 50
    ball.xv# = INITIAL_VELOCITY_BALL_X;
    ball.yv# = INITIAL_VELOCITY_BALL_Y;
    ball.width = GetSpriteWidth(ball.sprite)
    ball.height = GetSpriteHeight(ball.sprite)
    ball.framewidth = ball.width / 3
    SetSpriteOffSet(ball.sprite, ball.framewidth/2, ball.height/2)
    SetSpriteAnimation(ball.sprite, 28, ball.height,3)
    PlaySprite(ball.sprite, 5, 1, 1 , 2)
EndFunction


I forgot to mention my sprite is a sprite sheet, but I believe I'm doing the right thing here.

Here is the function to position the sprite.

+ Code Snippet
// -----------------------------------------------------------
// Render function
// -----------------------------------------------------------
Function render()

    SetSpritePositionByOffSet(ball.sprite, ball.x#, ball.y#)
    SetSpritePosition(player.sprite, player.x#, player.y#)
    ShowLogs()

EndFunction


And here is the place where I handle the ball movement.

+ Code Snippet
// -----------------------------------------------------------
// Ball movement handling
// -----------------------------------------------------------
Function HandleBallMove()
    ball.x# = ball.x# + ball.xv#
    ball.y# = ball.y# + ball.yv#
    if ball.x# > (SCREEN_WIDTH-(ball.framewidth/2))
        ball.x# = (SCREEN_WIDTH-(ball.framewidth/2))
        ball.xv# = ball.xv# * -1
        PlaySound(hit_sound)
        Log(">>>>>> x,xoffset,ball.x = " + Str(GetSpriteX(ball.sprite)) + "," + Str(GetSpriteXByOffSet(ball.sprite)) + "," + Str(ball.x#))
    else
        if ball.x# < (ball.framewidth/2)
            ball.x# = (ball.framewidth/2)
            ball.xv# = ball.xv# * -1
            PlaySound(hit_sound)
            Log(">>>>>> x,xoffset,ball.x = " + Str(GetSpriteX(ball.sprite)) + "," + Str(GetSpriteXByOffSet(ball.sprite)) + "," + Str(ball.x#))
        EndIf
    EndIf

    // Reaching the top means you made it!
    if ball.y# < (ball.height/2)
        gameState = 20
    EndIf
End Function


(*) Although it is called ball, it is actually a rectangular shaped sprite 38x28.

Thanks again!
Posted: 5th Oct 2011 21:42
Just a tip!

SetSpritePositionByOffset( bla,bla,bla) Will automatically offset to the center of the sprite, so no need to spend days calculating the center of your sprites with that old solar calculator on your desk, in the half dark light from your monitor, bashing and slamming it to make it work in the dim light.
Posted: 5th Oct 2011 21:50
MobileCreator, that log() function... does it write events to a logfile? Ifso, its a brilliant way to debug a game. Now, why did I not think of that?, with me printing diag strings all over the game screen...

Rectangular ball... sound like a ball squarepants bob would love!
Posted: 5th Oct 2011 22:23
Impetus73, you say
SetSpritePositionByOffset( bla,bla,bla) Will automatically offset to the center of the sprite,


but I don't think that is true. The help says

For example if the current offset is the center of the sprite this command will place the center of the sprite at the given coordinates.


and it has been my experience that the offset is used for the origin, NOT the center of the sprite. If you set the offset to the center, yes, it's positioned by the center. But if you set the offset to the bottom right corner, then it's positioned by that origin.
Posted: 5th Oct 2011 22:30
Don't correct me if i'm right, but it does work!

Try to rem out the setspriteoffset commands in your code, and it still produce the same results.

EDIT:
Rich, I see your point.

yes, the current offset. I just pointed out that the DEFAULT sprite offset is at the center of the sprite, if no setsprite offset command is used on the sprites.
Posted: 5th Oct 2011 22:36
Impetus73 - oh, I see what you are saying! If you want the offset to be the center, no need to actually set it.

We're on the same page now!
Posted: 5th Oct 2011 22:50
Yes! (hope it's not the last page)

I would a list from the TGC team, stating all the default values for the command functions, so we don't need setting alot of un-needed commands.
Posted: 5th Oct 2011 23:36
Hi,

Impetus73, the log function is to save to file and print on screen in a scrollable "window". I posted the sources here another day... I just didn't put a "Free Code" in the subject. The post is this one: http://forum.thegamecreators.com/?m=forum_view&t=189981&b=41

I'd love to get some feedback about it. Now back to my question:

So, if I want to *always* use the center of my sprite as my coordinates, I don't have to set anything, since it is the default. Is that correct?

Now, one thing that I don't understand is why GetSpriteXByOffSet() - GetSpriteX() is let's say 10 considering my sprite is 28 pixels wide and the offset is in the center of it. Shouldn't be the difference always 14?

Also, remember I'm not explicitly scaling my sprite.

Still lost here.

Thanks again!
Posted: 6th Oct 2011 0:32
darn, pressed F5 when i were going to post my reply, and all the text got F5kt... habbit from compiling, haha

Maybe the ball image is not in the center of the image, and the physic center is then used as default offset center?

+ Code Snippet
*********
*   **  *
*  *  * *
*  *  * *
*   **  *
*********
Posted: 6th Oct 2011 0:32
I thought your Sprite was 38x28 which would be 38 X by 28 Y.
Posted: 6th Oct 2011 2:45
Yes, you're right. I wrote that last post without checking my code.

So in any case, considering 38 wide, the middle is 19. The difference between x and offset x is 19, but I get around 9.

I'll try to test the same sprite in a blank project I see what I have. I'm pretty sure it is my mistake, not an AppGameKit bug, but I still have no idea what I've done.
Posted: 12th Oct 2011 20:57
Sorry to dig up an old horse to beat on but say if I use SetSpritePositionByOffset(spr,x,y), have a sprite of 10x10. Then let's say I set the Sprite using the Offset function to a position of 19X, 19Y, would that mean that the X position of the Sprite would be a 14 and the Y position be at 14?
Thanks, just trying to get a feel of how this function works.
Posted: 12th Oct 2011 21:47
This is true if you're setting the sprite offset right in the middle. As I learned in this thread, this is the default offset if you don't say explicitly otherwise.

There is a function called SetSpriteOffset that can be used to set the offset to another coordinate. So following your example, if I first call SetSpriteOffset(sprite, 2, 2) my sprite offset will be close to the upper left corner, and if I place it at 19x, 19y, the actual X and Y will be 17,17 instead.

Cheers