3

Context: I am using 2 sprites of 35x5 one on top of other.

Problem: On some event i am rescaling the top sprite to 50 %. It gets rescaled but its dimensions remains same and it lies in centre of lower sprite. Think of 2 bars. Top bar getting half and then setting in centre of bottom bar.

What i want: I want to set the starting position of top bar same to bottom bar. so that top bar resides at most left or most right.

What have i tried: On some event, i have called the below function but nothing happens except a fluctuation.

Bar.setPosition(SomeOtherSprite.getPosition().x, SomeOtherSprite.getPosition().y+SomeOtherSprite.getContentSize().height/2 + Bar.getContentSize().height/2+2);

Just for the info, that i have started on Cocos2d for Android some 2 days before. Putting a question in effort to get most material regarding Cocos2D in Android on Internet as i was unable to find except : Tutorial for Cocos in Android

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Farhan
  • 13,290
  • 2
  • 33
  • 59
  • yes anchor point was the solution, but i did it through hit and trial, didn't understand it thoroughly... – Farhan Oct 06 '11 at 07:28

1 Answers1

0

Anchor point is a point in a sprite local coordinate system (where (0, 0) is the bottom left corner and (1, 1) is top right). When you are setting sprite position, actually you are setting the position of the anchorPoint. Very convenient!

Andrew
  • 24,218
  • 13
  • 61
  • 90
  • after sprite.SetScaleX(0.5f); i have to do sprite.setAnchorPoint(1.0f,0.5f); in order to resolve my problem. Now how did this goes with the description of anchorpoint...???? – Farhan Oct 06 '11 at 11:21
  • 1
    @Farhan: http://www.qcmat.com/understanding-anchorpoint-in-cocos2d/ take a look here – Andrew Oct 06 '11 at 11:55