Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Contributor
Posts: 14
Registered: ‎12-23-2010
My Device: Playbook
Accepted Solution

Setting x=0

Hi,

I'm trying to position a sprite over another one, but when I try

x = (stage.stagewidth - second_sprite.width)/2

my second sprite is halfway out of the screen.

So I tried setting x=0 for the second sprite from the main sprite to see what it would do, and it is not all the way to the left.

Does anyone have any idea why?

 

I also have some buttons in the main sprite that won't align all the way to the left even with x=0.

 

Thanks!

 

Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: Setting x=0

You will need to post the actual code if you want us to help you.

 

This seems like a debugging task, just debug your app, put some breakpoints and check the values of all the variables you use. Also, check which Sprite is added to what object using addChild.

 

PS stage.stageWidth!

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Setting x=0

Dont count on Sprite's width and height being correct unless they have children.  They stay at 0 until a child is added.  If you're using Sprites as drawable entities, you may need to sub-class and keep your own attributes that control the dimensions of the object (like "size" or something).

 

Why x=0 is not pushing all the way to the left probably has to do with the container being drawn in.  Best to post sample code so we all can help.

Contributor
Posts: 14
Registered: ‎12-23-2010
My Device: Playbook

Re: Setting x=0

Hi,

Thanks for helping...

Here is the sample code that I put in my constructor:

 

public function MyApp()

{
            //set color for disabled buttons
            var format:TextFormat = new TextFormat();
            format.color = 0x0033FF;
            format.bold = true;
            
            for(var x:int = 1; x <= 9; x++)
            {
                for(var y:int = 1; y <= 9; y++)
                {
                    var lblbtn:LabelButton = this["lbl" + x.toString() + "_" + y.toString()] as LabelButton;
                    lblbtn.addEventListener(MouseEvent.CLICK, showNumPad);
                    lblbtn.setSize(35,35);
                    lblbtn.setTextFormatForState(format, SkinStates.DISABLED);
                    lblbtn.x = (((x%10)) - 1) * 40;
                    lblbtn.y = (y - 1) * 40;
                    addChild(lblbtn);
                }
            }

}

 

Also, on the button click event handler, I pop up another sprite, but when I do

othersprite.x = (originalsprite.stage.stagewidth - othersprite.width)/2, othersprite is not centered.

Not sure if I need to set the stage size somewhere?

 

Thanks a lot.

Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Setting x=0

[ Edited ]

I am not understanding what you're tyring to do with the line:

 

var lblbtn:LabelButton = this["lbl" + x.toString() + "_" + y.toString()] as LabelButton;

 

Are you trying to create a new button?  If so, it should be

 

var lblbtn : LabelButton = new LabelButton();

 

 

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Setting x=0

i think amy is trying to create a bunch of dynamic label buttons on the fly. however using that code is not going to do it. you're best be is creating an array that stores all of your label buttons. below is a sample code taken from an older post (slightly modified):

 

package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;

import qnx.ui.buttons.LabelButton;



[SWF(width="1024", height="600", backgroundColor="#CCCCCC", frameRate="30")]
public class LabelButtonTest extends Sprite
{
private var myButtons:Array;

public function LabelButtonTest()
{
super();

// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

/*
* Initialize your array of 2 X N (in our case we will be using 10)
*/

myButtons = new Array(new Array(), new Array());

/*
* Now we set up our array, first doing the first row and
* set up 10 buttons with their own ID using the constructor
* for our custom class CustomLabelButton(ID)
*/

for (var i:int = 0; i < 10; i++)
{
var myBtn:LabelButton = new LabelButton();

myBtn.label = "Button " + i;
myBtn.setSize(100, 50);

myButtons[0].push(myBtn);


}

/*
* We do the same for the second row of buttons, starting
* with 10
*/

for (var i:int = 10; i < 20; i++)
{
var myBtn:LabelButton = new LabelButton();

myBtn.label = "Button " + i;
myBtn.setSize(100, 50);

myButtons[1].push(myBtn);
}

/*
* Finally we cycle through our arrays and add them to our display
* this wil produce two rows of 10 buttons going accross
* each when clicked will call the funciton onClickEvent and
* trace out their ID to the debugger
*/

for (var i:int = 0; i < 10; i++)
{
var myBtn:LabelButton = myButtons[0][i];
var xPos:int = (i * 100) + 10;
var yPos:int = 10;

myBtn.setPosition(xPos,yPos);

addChild(myBtn);
}

for (var i:int = 0; i < 10; i++)
{
var myBtn:LabelButton = myButtons[1][i];
var xPos:int = (i * 100) + 10;
var yPos:int = 60;

myBtn.setPosition(xPos,yPos);

addChild(myBtn);
}

}
}
}

 

here is the thread discussing said code:

 

http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/How-to-create-array-of-controls-I...

 

hope that helps. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Contributor
Posts: 14
Registered: ‎12-23-2010
My Device: Playbook

Re: Setting x=0

 

JRab, you are right. I'm creating dynamic buttons on the fly. My code actually works - although your example is much better.

 

I had on the declarations all the labels which I did not copy in my snippet:

        internal var lbl9_1:LabelButton = new LabelButton();
        internal var lbl9_2:LabelButton = new LabelButton();
        internal var lbl9_3:LabelButton = new LabelButton();
        internal var lbl9_4:LabelButton = new LabelButton();
        internal var lbl9_5:LabelButton = new LabelButton();
        internal var lbl9_6:LabelButton = new LabelButton();
        internal var lbl9_7:LabelButton = new LabelButton();
        internal var lbl9_8:LabelButton = new LabelButton();
        internal var lbl9_9:LabelButton = new LabelButton();

 

But i now changed it to an array as you have suggested. Much better and easier to manipulate!

 

However, I am still having the problem that there is a space (i would say of a width of about 100) between the left of the screen and my first column of labelbuttons.

 

Any ideas?

 

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Setting x=0

hey,

 

glad u cut down the complexity!

 

as for the spacing if you kept the same code as you wrote in your previous post (with the added array) and it still looks the same i dont see any reason as to why your label buttons would be shifted. my only guess can be that where ever you are adding the label buttons (containing sprite) thats the problem. is it being added to the main application sprite?

 

and also to check for certain that the x position of the label button, trying doing a trace on all the label buttons being added to the screen. that'll tell you if its hte label button's coordinates that are messing you up or if its something else entirely. good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Contributor
Posts: 14
Registered: ‎12-23-2010
My Device: Playbook

Re: Setting x=0

So I finally figured out that the main Sprite was not taking up the whole stage (by setting opaqueBackground of the Sprite). When I set my controls in the Sprite to x=0, it was at x=0 of the Sprite, but not the stage.

 

I've tried to set the x of my Sprite to 0, but there is still a distance from the left side of the stage to the left side of the Sprite.

 

Would anyone know why?

 

Thank you!

Highlighted
Developer
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: Setting x=0

You might have to post some sample code for all of us to look at and reproduce.  It is probably something very simple, but without code, it is hard to pin point.  If you dont want to release your application code, create a new project that reproduces the problem.  Sometimes, just doing that will surface the problem for yourself.