public function resort():void {
while (tickets.numChildren > 0) {
tickets.removeChildAt(tickets.numChildren - 1);
}
for(var i:int = 0; i < ticketsBought.length; i++)
{
var t:TicketCard = ticketsBought[i] as TicketCard;
tickets.addChild(t);
}
}
after this function executes, the tickets are blinking visually, but I don't want that, is there a way to sort objects in the stage without blinking?
I think you can use the setChildIndex method of the DisplayObjectContainer class (see documentation) to reorder your objects without removing and then re-adding them.
Unfortunately I'm not in a position to test this at the moment, but I think the following should work:
public function resort():void {
for(var i:int = 0; i < ticketsBought.length; i++)
{
var t:TicketCard = ticketsBought[i] as TicketCard;
tickets.setChildIndex(t, i);
}
}
Related
I have a couple of display objects moving around the screen, the all belong to the same Vector.
I would like to create a function that lets me pick an object with the lowest x value at the moment the function is called.
This is quite easy and if you're into programming, you should be able to do it yourself, but if you're a beginner, I'll give you some code:
var vec:Vector.<DisplayObject> = new Vector.<DisplayObject>();
function getLowestXObject():DisplayObject
{
var minX:DisplayObject = vec[0];
for(var i:int = 1; i < vec.length; i++)
{
if(minX.x > vec[i].x)
{
minX = vec[i];
}
}
return minX;
}
does anyone know how to dynamically assign a name to NativeText / StageText as i am trying to build a dynamic interface instead of hardcoding it in, any help would be appreciated!
You should keep references to the dynamically created instances of StageText as you cannot get it by querying the display list (as it is not on display list). You can have the vector of of StageText
var m_vStageTextInstances:Vector<StageText>;
Then after you will just iterate over this vector.
Here's some example code that does what you want inside the iterate-function
public class IterationTest extends Sprite {
public function IterationTest() {
createObjects();
iterate();
}
private function createObjects():void {
for (var i:int = 0; i < 1000; i++) {
addChild(new TextField());
addChild(new MovieClip());
}
}
private function iterate():void {
var numTextObjects:int = 0;
for (var i:int = 0; i < this.numChildren; i++) {
var child:DisplayObject = getChildAt(i);
if (child is TextField) {
//do your stuff here
}
}
}
}
if anyone is trying to return text values with NativeText a StageText wrapper. they will need to edit;
https://github.com/cantrell/StageTextExample/blob/master/src/NativeText.as
add a public get function -
public function get text():String {
return this.st.text;
}
then you can return the NativeText values normally -
nt.text
I have an issue here. Every five seconds the spawnEnemies is fired. It creates a movieclip, sets it position and adds it to the stage and an array. In the function checkBullet I check if the bullet hits one of the enemies. If it does, the bullet and the enemy are removed from the stage, and the enemy is spliced from the array. But an error is thrown once I hit an enemy:
TypeError: Error #1010: A term is undefined and has no properties.
If I comment out the arrayEnemies.splice(i,1) line, it works fine but then it isn't removed from the array. This is in itself not an issue, but naturally I don't like to keep unnecessary data in my array. Any help on this?
function checkBullet(event:Event) {
if(stage.contains(mcBullet)) {
for(var i:int = 0; i < arrayEnemies.length; i++) {
if(arrayEnemies[i].hitTestPoint(mcBullet.x, mcBullet.y, true)) {
stage.removeChild(mcBullet);
this.removeChild(arrayEnemies[i]);
arrayEnemies.splice(i,1);
bulletOnStage = false;
}
}
if(mcBullet.x > 800) {
stage.removeChild(mcBullet);
bulletOnStage = false;
}
}
}
function spawnEnemies(event:TimerEvent) {
var enemie:MovieClip = new box_mc();
enemie.name = "mcBox" + event.currentTarget.currentCount;
enemie.x = 850;
enemie.y = Math.floor(Math.random()*(1+155)+255);
addChild(enemie);
arrayEnemies.push(enemie);
}
function moveEnemies(event:Event) {
for(var i:int = 0; i < arrayEnemies.length; i++) {
arrayEnemies[i].x -= 5;
}
}
This will be caused by working on an array that you are interating through, you should hold a ref to the stuff you want to remove then do it after the loop.
Your problem is that if the bullet hits two enemies, you try to remove it from the stage twice. This will throw an ArgumentError.
If you need to test against all enemies (assuming multiple enemies can be hit by the same bullet), you also need to decrement i when you remove an element from your enemy array.
function checkBullet(event:Event) {
if(stage.contains(mcBullet)) {
if(mcBullet.x > 800) {
stage.removeChild(mcBullet);
bulletOnStage = false;
}
for(var i:int = 0; i < arrayEnemies.length; i++) {
if(arrayEnemies[i].hitTestPoint(mcBullet.x, mcBullet.y, true)) {
if(stage.contains(mcBullet)){
stage.removeChild(mcBullet);
}
this.removeChild(arrayEnemies[i]);
arrayEnemies.splice(i,1);
bulletOnStage = false;
i--;
}
}
}
}
Bit of an older question but thought I'd throw in my answer too for anyone that might end up here.
Like Neil said, editing an array that you're itterating through (in this case arrayEnemies) can cause concurrent update problems.
My prefered solution is to use a seperate toRemove array and remove them after the update, for example:
var enemies:Array();
//Lets assume at some point this is populated with Enemy objects
function update():void
{
var toRemove:Array = new Array();
//May want to keep and clear an array instead to avoid creating a new one each update
foreach(var enemy:Enemy in enemies)
{
enemy.update();
if(enemy.markedForRemoval())
toRemove.push(enemy);
}
foreach(var enemy:Enemy in toRemove)
enemies.splice(enemies.indexOf(enemy), 1);
}
I'm making a game in flash and am using arrays to dynamically create items and place them inventory. LongSword is a MovieClip. I place the movieclip in the array like so:
function buyitem1(e:Event):void
{
if(Store.itemslot.length < 6 && Store.currentMenu == 1 &&score >= 450)
{
Store.itemslot.push(new LongSword);
}
}
Now I'm trying to remove the movieclip from the stage when the LongSword is "sold". How can i remove this longsword? I've tried:
for(var i:int = 0; i < Store.itemslot.length; i++)
{
if(Store.itemslot[i] == LongSword)
{
stage.removeChild(Store.itemslot[0]);
}
}
Ive also tried:
for(var i:int = 0; i < Store.itemslot.length; i++)
{
if(Store.itemslot[i] == new LongSword)
{
stage.removeChild(Store.itemslot);
}
}
and several variations. any ideas?
Try something like:
for each(var i:MovieClip in Store.itemslot)
{
if(i is Longsword)
{
var n:int = Store.itemslot.indexOf(i);
Store.itemslot.splice(n, 1);
if(i.parent) i.parent.removeChild(i);
break; // Only remove one Longsword.
}
}
If there are multiple instances of Longsword in the array, you may want to keep a reference to each instance somewhere for better comparison.
i have a few filters on a sprite. on mouse over i would like to access one of the filters in the filters array, but i'm having a bit of trouble trying to accomplish this.
mySprite.filters = [new DropShadowFilter(), new GlowFilter(), new BlurFilter()];
mySprite.addEventListener(MouseEvent.MOUSE_OVER, mouseOverEventHandler);
function mouseOverEventHandler(evt:MouseEvent)
{
//obtain indexOf the GlowFilter
trace(evt.currentTarget.filters[evt.currentTarget.filters.indexOf([Object GlowFilter])]));
}
the above code doesn't work. what's the proper way to get the index of a specific filter in a filters array?
If I understand correctly, you're essentially trying to do this:
var index:int = evt.currentTarget.filters.indexOf([Object GlowFilter]);
The bracketed part is not valid Actionscript it shouldn't even compile. What you need to do is to iterate over the filters and test them yourself since there's no way to search for a specific class with indexOf.
Try this instead:
function mouseOverEventHandler(evt:MouseEvent) {
var glowFilter:GlowFilter;
for (var i:int = 0; i < evt.target.filters.length; i++) {
if (evt.target.filters[i] is GlowFilter) {
glowFilter = evt.target.filters[i];
break;
}
}
}
Also, if you're going to fiddle with the filters in the array Flash won't accept in-place modifications, so you need to re-set the array once you've changed it:
function mouseOverEventHandler(evt:MouseEvent) {
var glowFilter:GlowFilter;
for (var i:int = 0; i < evt.target.filters.length; i++) {
if (evt.target.filters[i] is GlowFilter) {
glowFilter = evt.target.filters[i];
break;
}
}
if (!glowFilter) return;
glowFilter.blurX = 10;
var filters:Array = evt.target.filters;
filters[i] = glowFilter;
evt.target.filters = filters;
}