just for the record, i'm using AS3.
I have an issue where I would like to remove a sprite randomly in AS3, I have managed to figure out how to create the sprites so that they fill as a grid, just for the life of me I can't figure out how to remove them!
Here's the code i've used to create them:
function showpixels() : void
{
for (var i:int = 0; i < 40; i++)
{
for (var j:int = 0; j < 40; j++)
{
var s:Sprite = new Sprite();
s.graphics.beginFill(0);
s.graphics.drawRect(i*10, j*10, 10, 10);
s.graphics.endFill();
addChild(s);
pixels.push(s);
}
}
}
Basically I need these to be removed randomly until what's underneath can be seen.
Any help would be good, I'm pretty new to this! Thanks!
function removeRandom():void
{
var rand:uint = Math.random()*pixels.length;
var i:Sprite = Sprite(pixels[rand]);
if(i.parent) i.parent.removeChild(i);
pixels.splice(rand, 1);
}
UPDATE: To remove at random intervals you could try something like this:
var _timer:int = 100;
addEventListener(Event.ENTER_FRAME, _handle);
function _handle(e:Event):void
{
if(pixels.length > 0) _timer --;
if(_timer < 1)
{
_timer = 10 + Math.random()*50;
removeRandom();
}
}
function removeRandom():void
{
var rand:uint = Math.random()*pixels.length;
var i:Sprite = Sprite(pixels[rand]);
if(i.parent) i.parent.removeChild(i);
pixels.splice(rand, 1);
}
Marty's idea works. Another option would be to shuffle the array first and then just pop off elements.
To shuffle an Array use pixels.sort(function (...args):int { return int(2*Math.random()-1) }).
And then you can simple remove them like this:
function remove():void {
if (pixels.length) removeChild(pixels.pop());
else clearInterval(this.id);
}
And add this line at the end of showpixels:
this.id = setInterval(remove, 500);
Related
I've a array of list and created image place holder with the array objects name (Example: myArray[a,b]) and loaded images from there, having array list as folder name.
And finally created a movieClip to load on them and display in stage.
Now I want to remove them calling the same array list.
Need help here...
My code below:
for (var l: int = 0; l < ListArray.length; l++) {
var BookName: MovieClip = new bookThumb();
trace("\n[" + l + "]: " + ListArray[l]);
ImageFoldername = ListArray[l];
var bookImagePath: String = "file://" + File.userDirectory.nativePath.toString() + "/Books/" + ImageFoldername + "/Images/Icon.png";
trace(bookImagePath);
var ImagePlacer: Loader = new Loader;
var ImageURL: URLRequest = new URLRequest(bookImagePath);
ImagePlacer.load(ImageURL);
BookName.addChild(ImagePlacer);
BookName.name = ImageFoldername
addChild(BookName)
BookName.x = FirstBook_x;
BookName.y = FirstBook_y;
BookName.buttonMode = true;
BookName.mouseChildren = false;
BookName.addEventListener(MouseEvent.CLICK, IconSelected);
BookName.alpha = .8
BookName.addEventListener(MouseEvent.MOUSE_OVER, IconMouseOver);
BookName.addEventListener(MouseEvent.MOUSE_OUT, IconMouseOut);
FirstBook_x = FirstBook_x + 250;
}
Because you assign the ImageFoldername to each MovieClip's name property, you can use getChildByName to find them and remove them:
(Note: convention in AS3 is to use UpperCamelCase for classes, and lowerCamelCase for variables and functions. I've written the code below following this convention. )
for (var i:int = 0; i < listArray.length; i++) {
var imageFolderName:String = listArray[i];
var bookThumb:BookThumb = getChildByName(imageFolderName) as BookThumb;
if (bookThumb) {
removeChild(bookThumb);
}
}
Another way you can handle it is to add each book thumb into a list:
var bookThumbs:Array = [];
for (var i:int = 0; i < listArray.length; i++) {
var bookThumb:Bookthumb = new BookThumb();
bookThumbs.push(bookThumb);
// ...
}
Then to remove them all:
for each (var bookThumb:BookThumb in bookThumbs) {
removeChild(bookThumb);
}
bookThumbs.length = 0;
But probably the easiest way to remove them all would be to simply put them all in a single container and later use removeChildren() to remove them all:
var container:Sprite = new Sprite();
addChild(container);
for (var i:int = 0; i < listArray.length; i++) {
var bookThumb:Bookthumb = new BookThumb();
container.push(bookThumb);
// ...
}
// when you want to remove them all:
container.removeChildren();
I'm trying to do something like: If the column E has the word ok, column D gets formated as red. Seens preety easy, but something is wrong as I'm not entenring the if. Do u guys know what is wrong? I have read some other topics, but seens preety much the same
function onEdit() {
var ss =SpreadsheetApp.getActiveSheet();
var myRangeValues = ss.getRange('D7:E').getValues();
Browser.msgBox(myRangeValues);
for (var i = 0; i < myRangeValues.length; i++){
if(myRangeValues[i][0] == 'ok'){
myRangeValues[i][0].setBackground('red');
}
}
}
Thanks for the attention.
EDIT:
new code not working. I tried to put the logger.log in other lines as well. even without the logger, the error I get is that the function setBackground is impossible to find in the object.
function onEdit() {
var ss =SpreadsheetApp.getActiveSheet();
var myRangeValues = ss.getRange('D7:E').getValues();
for (var i = 0; i < myRangeValues.length; i++)
{
if(myRangeValues[i][1] == 'ok')
{
myRangeValues[i][0].setBackground('red');
//Logger.log("myRangeValues[i][1]: " + myRangeValues[i][1]);
}
}
}
You are mixing up methods.
value arrays don't have setBackground() method, this is a spreadsheet range method
use the code below to do what you want :
function onEdit() {
var ss =SpreadsheetApp.getActiveSheet();
var myRangeValues = ss.getRange('D7:E').getValues();
var myRangeColors = ss.getRange('D7:E').getBackgrounds();// get the colors
for (var i = 0; i < myRangeValues.length; i++)
{
if(myRangeValues[i][1] == 'ok')
{
myRangeColors[i][0]='#F00';
}
}
ss.getRange('D7:E').setBackgrounds(myRangeColors); //set the modified colors
}
I'm trying to add multiple copies of the same movieclip to the stage at once
I have the loop which fills the array and generate the movieclips to the stage
and the loop to add The click EventListener for each movieClip
but I miss the magical code to access every MovieClip separately
to have it removed from the stage by clicking on it
var numOfClips:Number = 5;
var mcArray:Array = new Array();
for(var i=0; i<numOfClips; i++)
{
var usd:mcUSD = new mcUSD();
//genrate random x , y position----------------------------
var randY:Number = Math.floor(Math.random()*460) + 120;
var randX:Number = Math.floor(Math.random()*350) + 60;
usd.x = randX;
usd.y = randY;
//---------------------------------------------------------
mcArray.push(usd);
addChild(usd);
}
for(var m:int = 0; m<mcArray.length; m++){
usd.addEventListener(MouseEvent.CLICK, colectmoney);
}
function colectmoney(e:MouseEvent): void {
removeChild(usd);
}
Try this:
import flash.events.MouseEvent;
var numOfClips:Number = 5;
var mcArray:Array = new Array();
for(var i=0; i<numOfClips; i++)
{
var usd:mcUSD = new mcUSD();
//genrate random x , y position----------------------------
var randY:Number = Math.floor(Math.random()*460) + 120;
var randX:Number = Math.floor(Math.random()*350) + 60;
usd.x = randX;
usd.y = randY;
//---------------------------------------------------------
mcArray.push(usd);
addChild(usd);
}
addEventListener(MouseEvent.CLICK, mouseClickHandler);
function mouseClickHandler(e:MouseEvent) : void {
removeChild(mcArray[mcArray.indexOf(e.target)]);
}
Important things to note: 1) you do not need to call a mouse.click event listener for each mcUSD object. It's more efficient to call it once. 2) removeChild(usd) won't work because you need to tell AS3 which mcUSD object to remove. 3) try to keep function nomenclature consistent - eg colectMoney instead of colectmoney. it will save you confusing times once your program gets bigger. hope this helps! :)
How can I splice an array within another array?
I'm trying to create a game for kids in my class. Some kind of a trivia history question creator. How to splice the MasterArray so that I would get rid of sub-arrays (Hard-England and Medium-England) within the allEnglandArray. Because the "MasterArray.splice()" seem to be affecting - splicing only the allEngland array, or the allFrance array. But I need to get rid of those sub-arrays...
My code:
var Easy-England:Array = ["item1","item2","item3","item4","item5"];
var Medium-England:Array = ["item6","item7","item8"];
var Hard-England:Array = ["item9","item10"];
var allEngland:Array = [Easy-England,Medium-England,Hard-England];
var Easy-France:Array = ["item11","item12","item13","item14","item15"];
var Medium-France:Array = ["item16","item17","item18"];
var Hard-France:Array = ["item19","item20"];
var allFrance:Array = [Easy-France,Medium-France,Hard-France];
// the list of countries goes on and on and on... (Italy, Hungary, etc.)
var allStuff:Array = [allEngland, allFrance, etc.];
var MasterArray:Array;
// FUNCTIONS
// clear MasterArray - first I clear out completely the MasterArray
function clearMasterArray():void
{
MasterArray.splice(0);
}
// update MasterArray - than I fill the MasterArray with data according to checkBoxes
function updateMasterArray():void
{
for (var i:int = 0; i<checkBoxBlock.myCheckBoxes.length; i++)
{
if (checkBoxBlock.myCheckBoxes[i].selected)
{
MasterArray.push(allStuff[i]);
}
}
}
// splice MasterArray - last thing I do is splice the items according to student's proficiency level, referred to as "studentPL".
function spliceMasterArray():void
{
if (studentPL == 1)
{
for (var i:int = 0; i<allStuff.length; i++)
{
allStuff[i].splice(5,5);
}
}
if (studentPL == 2)
{
for (var i:int = 0; i<allStuff.length; i++)
{
allStuff[i].splice(8,2);
}
}
if (studentPL == 3)
{
for (var i:int = 0; i<allStuff.length; i++)
{
trace("no need to splice");
}
}
}
And after this I call those functions in another function in this order...
function creatorFunction():void
{
clearMasterArray();
updateMasterArray();
spliceMasterArray();
}
Instead of:
var allEngland:Array = [Easy-England,Medium-England,Hard-England];
try this:
var allEngland:Array = Easy-England.concat(Medium-England, Hard-England);
This way you will have a 'flat' array (no sub-arrays), so it will be easier to deal with.
I have been trying to do this for two nights now and haven't had any joy, please help if you can...
Simple throwing game with darts and other weapons, what I am trying to do is
Remove an array and all of its Children when I change weapon,
I feel sure there is a simple snippet of code that will remove all the children and array with no hassle but I haven't yet figured it out, if you know or could suggest anything, I would really appreciate it.
something like "removeArrayAndAllInstances(balls);" if only...
at the moment I have....(balls is the array in question)
for(var inter:int = balls.length - 1; inter > -1; inter--)
{
balls.splice(1);
balls.splice(1, balls.length);
}
but this docent work for some reason, the array and all of its children are all still on the stage.
I also tried
balls[];
No luck...
Please don't judge my code I am a novice as I am sure was evident and I know its a disgusting mess, sorry (Its the only way it makes sense to me).
I have tried numerous things, hope someone can help
Thanks in advance.....
var mouseTarget:MovieClip;
var balls:Array = new Array();
var ball:MovieClip = new dart();
var hammers:MovieClip = new hammer();
ball.x = 150;
ball.y = 50;
hammer_btn.addEventListener(MouseEvent.MOUSE_DOWN, hammerweapon);
dart_btn.addEventListener(MouseEvent.MOUSE_DOWN, dartweapon);
function removealldartsfromstage(e:MouseEvent):void
{
for(var inter:int = balls.length - 1; inter > -1; inter--)
{
balls.splice(1);
balls.splice(1, balls.length);
}
stage.removeEventListener(MouseEvent.MOUSE_UP, addDart);
}
function dartweapon(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, addHammer);
dart_btn.removeEventListener(MouseEvent.CLICK, dartweapon);
stage.addEventListener(MouseEvent.MOUSE_UP, addDart);
//removeChild(balls);
}
function hammerweapon(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, addDart);
dart_btn.addEventListener(MouseEvent.MOUSE_DOWN, dartweapon);
//stage.addEventListener(MouseEvent.MOUSE_UP, addDart);
stage.addEventListener(MouseEvent.MOUSE_UP, addHammer);
}
function addHammer(e:MouseEvent):void
{
var hammers = new hammer();
addChild(hammers);
removeChild(dart_btn);
addChild(dart_btn);
dart_btn.addEventListener(MouseEvent.CLICK, dartweapon);
balls.splice(10);
}
function addDart(e:MouseEvent):void
{
str.alpha = 0;
var ball = new dart();
addChild(ball);
removeChild(hammer_btn);
addChild(hammer_btn);
ball.x = 150;
ball.y = 50;
balls.push(ball);
trace(balls);
addEventListener(Event.ENTER_FRAME, checkIfHitTest);
hammer_btn.addEventListener(MouseEvent.MOUSE_DOWN, removealldartsfromstage);
function checkIfHitTest(Event)
{
for (var i:int = 0; i<balls.length; i++)
{
if (balls[i].dart_point.hitTestObject(eyeleft))
{
trace("hitleftbullseye");
ball.gotoAndStop("hitlefteyeframe");
Event.currentTarget.removeEventListener(Event.type, checkIfHitTest);
balls.splice(i, 1);
}
}
}
}
The balls array is just a storage for ball references. It bears no relation to the stage or the DisplayObjectContainer which they have been added at all. So you have to remove them individually.
This is what I'd do:
while(balls.length > 0)
{
var ball:MovieClip = balls.pop();
if (ball.parent) // Just to make sure you are referencing the correct container.
{
ball.parent.removeChild(ball);
}
}
I can't follow the logic of your game very well. That said, you'd do well to create conatiners for separate group of clips in order to make managing them easier. For example, in the creation stage:
var ballContainer:Sprite = new Sprite();
addChild(ballContainer);
for (var i:int = 0; i < ballLimit; i++)
{
var ball:Dart = new Dart();
ballContainer.addChild(ball);
}
This way, you can clear them of children all at once:
function removeAllChildren(container:Sprite) // Or just DisplayObjectContainer
{
while(container.numChildren > 0)
{
container.removeChild(container.getChildAt(0));
}
}