using Action Script 3 remove an object that appeared in the run time - actionscript-3

I use this code to create list of words from an array and make them act like buttons
in the main page i have 2 buttons that activate the for loop to create the list of words
the problem is when i hit the second button in the main the list of words of the first button still shown on the screen, i need to remove the list first then activate the second loop
i used the removeChildAt() function but it didn't worked
what should i do ?
var words:Array = [];
var word:MovieClip=new MovieClip();
if(e.target.name=="glossumbolA"){
words=['School1','School2','School3','School4','School5','School6']
for (var j:int = 0; j < words.length; j++)
{
word = createTextButton(words[j]);
addChild(word);
word.addEventListener(MouseEvent.CLICK, wordButtonClick);
}
}
else if(e.target.name=="glossumbolB"){
words=['Class1','Class2']
for (var i:int = 0; i < words.length; i++)
{
word = createTextButton(words[i]);
addChild(word);
word.addEventListener(MouseEvent.CLICK, wordButtonClick);
}
}
function createTextButton(){
//code to make the word in the array a button
}
function wordButtonClick(){
//code to make an action when the word in the array clicked
}

you have multiple ways to do that:
1. create one new MovieClip inside 'word' (with name A), for glossumbolA and one new for glossumbolB (with name B), create buttons inside MCs. You can set 'visible' true || false for A and B. If glossumbolA button is clicked - check if B is visible, set visible to 0.
or
while (word.numChildren > 0) {
word.removeChildAt(0);
} then create new buttons

Related

Removing items of an array from stage in as3- one each time

I am trying to remove movieclips of an array from the stage. I want to remove one item of the circles array every time there is a click in an item from the square array. I wrote this code but something is missing and i can not figure it out because all the items of the circles array disappear from stage with the first click. Can you please help me.
var circles:Array = [circle1, circle2, circle3, circle4,circle5, circle6];
var counter:int = 0;
var square:Array = new Array(square1,square2,square3,square4,square5,square6,square7,square8,square9,square10,square11,square12,square13,square14,square15,square16,square17,square18,square19,square20,square21);
for(var i:int = 0; i < square.length; i++)
{
square[i].addEventListener(MouseEvent.CLICK, clickTomove);
function clickTomove(e:MouseEvent):void
{
trace("square");
var len:int=circles.length;
for(var o:int=0; o<circles.length; o++)
this.removeChild(circles[o]);
circles.splice(o,1);
}
}
Your click handler loops through the circles and removes them all, so what you're describing is expected.
You shouldn't put function declarations inside a loop.
It looks like you're trying to do something like this:
for each (var square:MovieClip in squares) {
square.addEventListener(MouseEvent.CLICK, clickTomove);
}
function clickTomove(e:MouseEvent):void {
if (circles.length > 0) {
var circle:MovieClip = circles.pop();
removeChild(circle);
}
}
pop() removes and returns the last item from the circles array.

how do I call a function if the image clicked is equal to its matching word? Actionscript3

When my random word appears the user has to memorise it and click the correct corresponding image to it. I'm trying to write the code that runs if the user selects the right image. I have paired my words and images in my array. I'm just unsure as how to go about calling this function.
This is what I've attempted so far, but this isn't working. I'm new to actionscript3 so excuse the lack of knowledge as I am trying to teach myself.
All help greatly appreciated!!
This is one way you can do this:
See code comments
basket.visible = false;
//------ Home Button ------\\
backhome1btn.addEventListener(MouseEvent.CLICK, goback1Click);
function goback1Click(event:MouseEvent):void{
gotoAndStop("homepage");
}
//-------------------
var score:int = 0;
var items:Array = new Array(); //store all food items in array
var wordsToShow:Array = new Array(); //store all words to show in array - this is the array that will keep track of which has been asked (or rather not asked yet)
//to reduce redundant code, call this with each food item (below)
function initFoodItem(item:MovieClip, word:String):void {
item.word = word; //forget the array, just store the word on the image as a dynamic property
item.addEventListener(MouseEvent.CLICK, foodClicked);
items.push(item); //add to array
wordsToShow.push(item); //add to array
item.visible = false;
}
initFoodItem(oc, "Orange Juice");
initFoodItem(sand, "Sandwich");
//...repeat for all other food items
//now randmize the words to show array:
wordsToShow.sort(function(a,b):int {
return(Math.random() > .5) ? 1 : -1;
});
var curAnswer:MovieClip; //a var to store the current correct answer
//this does the next question per se
function displayWord():void {
if(wordsToShow.length < 1){
//they've all been asked
gotoAndPlay("gameoverpage");
return;
}
curAnswer = wordsToShow.pop(); //assigns the last item in the array to the cur asnwer, and pop also removes that item from the array (so it won't get asked again)
randomword.text = curAnswer.word; //assign the text to the word value of this item
randomword.visible = true;
remember.visible = true;
}
remember.addEventListener(MouseEvent.CLICK, readyClick);
//when you click your ready button
function readyClick(e:MouseEvent):void {
//we have an array of all items, let's loop through it and change them all to be visible
for (var i:int = 0; i < items.length; i++) {
//items[i].alpha = 1; //alpha values are 0 - 1 in AS3
items[i].visible = true; //use visible instead of alpha if just toggling visibility, it's more efficient
}
randomword.visible = false;
remember.visible = false;
bask.visible = true;
notepape.visible = false;
//! another reason to use visible instead of alpha = 0, is alpha = 0 items will still be clickable! visible = false items cannot be clicked.
}
function foodClicked(e:MouseEvent):void {
if (e.currentTarget == curAnswer) {
//if the current target (the item clicked) is the same item as what we stored in curAnswer, you answered correctly, so do this:
score += 10; //or however much you get for answering correctly
gotoAndStop("listpage"); //not sure what this does?
displayWord();
}else {
//wrong
gotoAndStop("gameoverpage");
}
}

Display of current and past actions threw text

So iv set up a set of text to display the last actions you'v done within my game. Iv managed to get it to add the text below the current text, but how would i move all of the previous text upwards so each time it adds a new text it will place it in the same spot and only move the previous text, then delete the oldest text after a certain amount has been displayed
This is the game
http://www.fastswf.com/F7ei81E
this is the code im currently using
protected function createEventText()
{
chatScreen = new Sprite();
chatScreen.x = 280;
chatScreen.y = 520;
addChild(chatScreen);
addText("The world has spawned!");
}
public function addText(t:String)
{
eventArray.push(t);
for (var i:int = eventArray.length - 1; i < eventArray.length; i++)
{
et = new EventTextMC();
et.txt.text = String(eventArray[i]);
et.y = i * 20;
chatScreen.addChild(et);
}
}
What I suggest is that you keep a finite number of EventTextMC instances on your chatScreen. Then, when you add the text, you can "push" the text from the top field to the bottom field.
private var _historyLength:int = 5; // number of text fields to generate
private var _textFields:Array = []; // array to keep track of textfields
// create the finite number of fields (based on historyLength)
private function initLog():void
{
// adding 5 (historyLength) text fields to the chatScreen
for(var i:int = 0; i < historyLength;i++)
{
var et:EventTextMC = new EventTextMC();
et.y = i * 20;
textFields.push(et);
chatScreen.addChild(et);
}
}
public function addText(t:String)
{
eventArray.push(t); // this array isn't actually being used in this example
// starting with the last text field, copy the previous field's text
// this will create a "pushing down" effect
// CLARIFICATION:
// The text from the second to last field will be placed into the last field.
// The text from the third to last field will be placed into
// the second to last field.
// This will continue until the text from the first field will be placed
// in the second field, freeing up the first field for the new text.
for(var i:int = historyLength-1; i > 0; i--)
{
textFields[i].txt.text = textFields[i-1].txt.text;
}
// add the new text to the first (top-most) textfield
textFields[0].txt.text = t;
}
While I'm not actually using the eventArray in this example, you could easily use it later on to "scroll" through the logs.

How to swap bitmap in a symbol use jsfl

I want to swap a bitmap in symbol with another bitmap,and I found the the function
swapElement
This is what I have tried
var elements = item.timeline.layers[0].frames[0].elements;//item is the symbol
for (var j = 0; j < elements.length; j++) {
var el = elements[j];
if (el.instanceType == "bitmap") {
el.selected = true;//this line not work, so I want a way to make the element selected
dom.swapElement(targetName);//targetName is another bitmap name that I wanted
}
}
Setting selected to true of an element, will only select the element if the element is on screen (and the layer is unlocked and visible). So before selected is set to true library.editItem(item.name) should be called. If the element is in frame x, frame x has to be made the current frame with document.getTimeline().setSelectedFrames(parseInt(x), parseInt(x) + 1, true). (I use parseInt(x) as a cast because of a bug in setSelectedFrames). To be sure the element is the only element that is selected document.selectNone() should be called before setting selected to true.
Every instance has a libraryItem so maybe replacing the libraryItem is easier.
The code here can be adapted for a more selective replace.
From library? Then following snippet could help you
var libItems = fl.getDocumentDOM().library.items;
for (i = 0; i < libItems.length; i++){
if(libItems[i].itemType == “bitmap”){
//found bitmap :)
}
OK, what about:
fl.selectElement(el); //instead of el.selected = true;
dom.swapElement(targetName);//assuming you have targetName
This works if there is anything (one item) selected that is bitmap it will replace it with library item named "image4":
var dom = fl.getDocumentDOM();
var selection = dom.selection;
if(selection.length == 1 && selection[0].instanceType == "bitmap")
{
dom.swapElement("image4");
}

Dynamic text within one UILoader in ActionScript-3

I want to show some dynamic text and that is read from a php file within xml. Here in swf player i just set three button. Two button for text and one button for images. This all are working fine but the swf background covered by the white background. Here is the normal view and some of this code snippet
normal view image http://outshinebd.com/sm/Flash/normal_view.png http://outshinebd.com/sm/Flash/normal_view.png
Code :
btnItem.addEventListener(MouseEvent.CLICK, showItem);
//btnItem.addEventListener(Event:event, showItem);
function showItem(event:Event):void
{
imgLoader.alpha =0;
textLoader.alpha=0;
imgLoader3.alpha=0;
imgLoader4.alpha=0;
imgLoader5.alpha=0;
imgLoader2.alpha=0;
var itemLoader:URLLoader = new URLLoader();
itemLoader.load(new URLRequest("http://localhost/sm/flash/productdata"));
itemLoader.addEventListener(Event.COMPLETE , onItemLoad);
function onItemLoad(e:Event):void
{
var myLoader:XML = new XML(e.target.data);
xmlList = myLoader.children();
//this values array will hold all of the values we need to paginate
var values:Array = new Array();
//defining the 'i' variable so we can use it multiple times in for loops
var i:int;
for(i = 0; i < xmlList.length(); i++){
//textLoader.text = xmlList[i].elements("productname");
values[i] = xmlList[i].elements("productname");
}
//the current page we're on
var page:int = 1;
//the limit of values we need per page
var limit:int = 1;
//the current row that we're working on
var row:int = 0;
//The total pages that we have
var totalPages:int = Math.ceil(values.length/limit);
function createValues():void{
//this will always limit the amount to the limit variable
//but, "i" will act as a marker for which part of the values
//array that we're going to use
for(i=(page-1)*limit;i<page*limit;i++){
//checks if there actually is a value where "i" is
//otherwise we'll get some undefined movieclips
if(i < values.length){
var newValue:UILoader = new UILoader();
//sets the coordinates based on the row
newValue.x = 5;
newValue.y = 5+newValue.height*row;
//sets this guys value to the correct part of the array
textLoader.text = values[i];
//changing this guys name so we can reference it later
newValue.name = 'value'+row;
//adds the mc to stage
addChild(newValue);
//move onto the next row
row ++;
}
}
//then we reset the rows so we can use the variable again
row=0;
}
//function to remove the mc's
function removeValues():void{
//this loop will run once for each mc on stage
for(i=0;i<limit;i++){
//get the object in the current row and kill it!
removeChild(getChildByName('value'+i));
}
}
//next page and previous page functions
function prevPage(event:MouseEvent):void{
//checking if the current page isn't too low
if(page > 1){
//take away all of the objects so we can make new ones
removeValues();
page --;
createValues();
//then update the page text
//updateString();
}
}
function nextPage(event:MouseEvent):void{
//checking if the current page isn't too high
if(page < totalPages){
removeValues();
page ++;
createValues();
//updateString();
}
}
//then we place the movieclips onto the stage
createValues();
//adding listeners to the buttons
btnPrev.addEventListener(MouseEvent.CLICK, prevPage);
btnNext.addEventListener(MouseEvent.CLICK, nextPage);
}
}
And the view after clicking the button
after clicking image http://outshinebd.com/sm/Flash/after_click.png http://outshinebd.com/sm/Flash/after_click.png
I'm stacked with in the last two days. Please give me a solution.
It seems that you are using an input TextField or a TextTield with a border and a background, just turn them off:
textLoader.border=textLoader.background=false;