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
New Contributor
evgenyvinnik
Posts: 4
Registered: ‎12-14-2010
My Device: Not Specified
Accepted Solution

How to create array of controls (Images, Buttons) in ActionScript

Hello!

 

Could anyone help with with ActionScript and Playbook SDK?

 

How can I create two-dimensional array of controls (buttons, images, text fields) in ActionScript?

And how I can use radio buttons in ActionScript?

Please use plain text.
Developer
gluth
Posts: 192
Registered: ‎09-23-2009
My Device: 9860/PlayBook/Dev Alpha
My Carrier: Plus GSM

Re: How to create array of controls (Images, Buttons) in ActionScript

Maybe you should try using list and adding more columns to it?

 

Adding a viewable 2nd column to List - BlackBerry Support Community Forums

 

It depends what do you want to archieve with your controls.

Found my post helpfull? Like it! It solves your trouble? Help others and accept it as solution. Search before asking.
My apps: Secret Cod3s | Should my tablet be banned?
--
Jakub Nietrzeba
PGS Software S.A.
Your reliable partner in Poland - IT Outsourcing - BlackBerry, iPhone, Web and Standalone applications
Please use plain text.
New Contributor
evgenyvinnik
Posts: 4
Registered: ‎12-14-2010
My Device: Not Specified

Re: How to create array of controls (Images, Buttons) in ActionScript

I want to create two rows of buttons and each button should have their own id. All of the buttons will call one OnClick method, where I will detect button id and then process button click.

Please use plain text.
Developer
gluth
Posts: 192
Registered: ‎09-23-2009
My Device: 9860/PlayBook/Dev Alpha
My Carrier: Plus GSM

Re: How to create array of controls (Images, Buttons) in ActionScript

From my POV it looks like the best idea will be to create them manually, but it is your choice, you know your application the most.

Found my post helpfull? Like it! It solves your trouble? Help others and accept it as solution. Search before asking.
My apps: Secret Cod3s | Should my tablet be banned?
--
Jakub Nietrzeba
PGS Software S.A.
Your reliable partner in Poland - IT Outsourcing - BlackBerry, iPhone, Web and Standalone applications
Please use plain text.
Developer
Ebscer
Posts: 875
Registered: ‎08-31-2009
My Device: 9530, 9630, 9800, 8530, 9900, 9810, 9930, PlayBook, Dev Alpha

Re: How to create array of controls (Images, Buttons) in ActionScript

You are thinking in a bit too much of a Java mindset.

 

In actionScript it is better just have a button click on each button launch a different method, so that you don't need to do any filtering based on button name or anything like that.

 

You can do things like what is shown below.

 

 

var newGameButton:LabelButton = new LabelButton();
newGameButton.label = "New Game";
newGameButton.addEventListener(MouseEvent.CLICK, newGame);

var restartGameButton:LabelButton = new LabelButton();
restartGameButton.label = "Restart Game";
restartGameButton.addEventListener(MouseEvent.CLICK, restartGame);

private function newGame(event:Event):void
{
    //new game code
}

private function restartGame(event:Event):void
{
    //restart game code
}

 

 


Read my thoughts on BlackBerry Development at news.ebscer.com
Please use plain text.
New Contributor
evgenyvinnik
Posts: 4
Registered: ‎12-14-2010
My Device: Not Specified

Re: How to create array of controls (Images, Buttons) in ActionScript

I was thinking about creating method for each button, but I am afraid in my case that would be lots of ugly code:

about 20 almost identical functions.

 

And also I need to create gaming field: that would be about 100 tiles that can change color, so I thought having an array would ease the way I handle them.

 

I am really from C#, Visual C++ background and I was thinking about the easiest way to do that.

Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: How to create array of controls (Images, Buttons) in ActionScript

hey,

 

buttons and components do not have ID's like in html and javascript. that being said its not impossible to have it that way. the best approach is to create your own class that extends the LabelButton (or whatever component you need) and have an id property set up. so you can set a custom id to it. i've implemented what you've asked below. you can apply this to other components as well jsut have to do modifications to the class and edit the extension. here's the code:

 

LabelButtonTest.as (Main application class):

 

 

package
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	
	[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:CustomLabelButton = new CustomLabelButton(i);
				
				myBtn.label = "Button " + i;
				myBtn.setSize(100, 50);
				
				myBtn.addEventListener(MouseEvent.CLICK, onClickEvent);
				
				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:CustomLabelButton = new CustomLabelButton(i);
				
				myBtn.label = "Button " + i;
				myBtn.setSize(100, 50);
				
				myBtn.addEventListener(MouseEvent.CLICK, onClickEvent);
				
				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:CustomLabelButton = 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:CustomLabelButton = myButtons[1][i];
				var xPos:int = (i * 100) + 10;
				var yPos:int = 60;
				
				myBtn.setPosition(xPos,yPos);
				
				addChild(myBtn);
			}
			
		}
		public function onClickEvent(e:MouseEvent):void
		{
			/*
			 * Print out ID of the button thats clicked
			*/
			trace("the button ID is: " + e.target.id);
		}
	}
}

 

 

CustomLabelButton.as (Our custom label button class with ID property):

 

 

package
{
	import qnx.ui.buttons.LabelButton;
	
	public class CustomLabelButton extends LabelButton
	{
		private var _id:int;
		
		public function CustomLabelButton(num:int = 0)
		{
			super();
			
			id = num;
			
		}
		public function set id(num:int):void
		{
			_id = num;
		}
		public function get id():int
		{
			return _id;
		}
	}
}

 

run the code and see how it works. hope this is what you are looking for. 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
Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10

Re: How to create array of controls (Images, Buttons) in ActionScript

I believe the flash.display.DisplayObject.name property is exactly what you would want to use as an "id" for each button.  Just create them in a loop, assigning to button.name as you go.  There's even a handy DisplayObjectContainer.getChildByName() call for you to use to find them, if you happen not to have stored a reference.

 

Alternatively, you could store actual references to the buttons, and check which button you have as the target of the event when you click on it.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
New Developer
cookeit
Posts: 27
Registered: ‎12-11-2010
My Device: Not Specified

Re: How to create array of controls (Images, Buttons) in ActionScript

I've been using the following to attach an ID to my buttons:

 

prompt.setButtonPropertyAt("id", object, index);

 http://www.blackberry.com/developers/docs/airapi/1.0.0/qnx/dialog/BaseDialog.html#setButtonPropertyA...

 

Please use plain text.
Developer
fnicollet
Posts: 85
Registered: ‎10-25-2010
My Device: Not Specified

Re: How to create array of controls (Images, Buttons) in ActionScript

Hi,

Maybe i get your question wrong, but if you need a gaming tile (let's say like a chess game) with interactive tiles, creating Buttons ain't the right solution cause it will add to much overhead.

What i would do would be creating some Sprite objets:

http://livedocs.adobe.com/flash/9.0_fr/ActionScriptLangRefV3/flash/display/Sprite.html

This is basic AS3 and the most light-weight objet you will need that can dispatch events.

And before creating your own game engine, i suggest you should look at the existing librairies that will do most of the annoying work :smileywink:. Here is a nice list:

http://www.flashrealtime.com/flash-game-library-engine-list/

Hope this helps,
Fabien

--
1. If you liked my post or found it useful please click on the white star and provide a Kudo!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!
Please use plain text.