- Home /
Convert gameObject position on screen
Hello, I would like to create 6x6 board with toggleable sprites. They should be next to each other. On andEngine I base on anchorCenter which is (.5f,.5f) by default in every new object.
On andEngine I arrange it like this
for (int i = 0; i < BOARD_ROW; i++) {
for (int j = 0; j < BOARD_COLUMN; j++) {
Letter rLetter = rollLetter ();
float texWidth = rLetter.sprite.rect.width;
float texHeight = rLetter.sprite.rect.height;
Debug.Log (texWidth + "x" + texHeight); // 50 x 50
//Here I'm not sure which convert I should use. 2.5f because it's 2.5 texture width (50*2.5) form layer/scene center. It would be .5 if I want 2x2 board.
rLetter.position = Camera.main.ScreenToWorldPoint (new Vector2 ((-(texWidth * 2.5f) + (texWidth * j)), (texHeight * 2.5f) - (texHeight * i)));
lettersOnScreen.Add (rLetter);
}
}
And I got some thing like this
I can live with (0,0) on down left corner but space between sprites is too big.
Letter class
class Letter
{
private GameObject gameObject;
public Vector2 position {
get {
return this.gameObject.transform.localPosition;
}
set {
this.gameObject.transform.localPosition = value;
}
}
public Sprite sprite {
get;
set;
}
public BoardPosition boardPosition {
get;
set;
}
public float likelihood {
get;
set;
}
public Range likelihoodRange {
get;
set;
}
public string code {
get;
set;
}
public bool clicked {
get;
set;
}
public Letter ()
{
}
public Letter (GameObject go)
{
this.gameObject = go;
}
public Letter (Sprite sprite, string code, float likelihood, Range likelihoodRange)
{
this.sprite = sprite;
this.code = code;
this.likelihood = likelihood;
this.likelihoodRange = likelihoodRange;
}
BoardPosition getBoardPosition ()
{
return boardPosition;
}
void setBoardPosition (BoardPosition boardPosition)
{
this.boardPosition = boardPosition;
}
}
Can you tell me what I'm doing wrong?
Rather than walk through your code, let me make an alternate suggestion about how to do this layout.
For me when I doing something like this, I like to work in a unit grid in world space. I can greatly simplify things like hit detection and movement and in general is just easier to visualize when I'm writing code. So select the texture(s) you are using for the sprite and set the 'Pixels to Units' in the Inspector. For example if your textures are each 50 x 50, and you want them to completely fill the unit space, set the 'Pixels to Units' to 50. If you want a bit of space between them, then set it to something like 54. Then write your code so that it places things one unit apart in world space...no conversion necessary. Adjust the camera orthographic size or field of view as needed.
Thanks... I go step ahead and after setting pixelToUnity to 50 I create prefab with all needed components :) Now it's peace of cake :)
hi,
I'm trying to do something like that. But I do not understand what you tried to explain in the comment ...
/ / Here I'm not sure which convert Should I use. 2.5f because it's 2.5 texture width (50 * 2.5) form layer / scene center. It would be .5 if I want 2x2 board.
Could you explain me better?
$$anonymous$$y game has the gameplay of this game, http://s.glbimg.com/jo/g1/f/original/2012/04/05/sem-titulo-2_1.jpg where you need to form a word with the letters. And meumaior problem is in the organization of words, because I need to organize them on the mobile screen.
@victorbnunes - I'm not my suggestion above the the right one for your app. In general, you want to decide on a world size for your sprite. In my comment above, I was encouraging each tile to be one world unit wide, but you can select any width. I only encourage a one unit witch because it simplifies layout code and it can simplify hit testing. Say you wanted each time to be 1.5 units wide. Next you need your texture width. You say it is 50 pixels. So then if you wanted all the letter tiles flush against each other you would use 33.3333 pixels per unit for the sprite (50 / 1.5 = 33.33333).