error With changing x and y positions of movieclip using array - arrays

I'm trying to create a program using Action Script 3.0 where I click an element, click on another element, the first element goes to the second element.
This is My code:
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.MovieClip;
import flash.events.MouseEvent;
stop();
var Plchange:MovieClip;
var As:Array = new Array(A1,A2,A3,A4,A5,A6,A7,A8,A9);
var Bs:Array = new Array(B1,B2,B3,B4,B5,B6,B7,B8,B9);
var Cs:Array =new Array(C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12);
var Cs1:Array=new Array(C13,C14,C15,C16,C17,C18,C19,C20,C21,C22,C23,C24);
for ( var i:int =0; i<=8 ;i++)
{
As[i].addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{ gotoPlace(me, As[i])});
Bs[i].addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{ gotoPlace(me, Bs[i])});
}
for ( var Y:int =0; i<=12 ;Y++)
{
Cs[Y].addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{ Set(me, Cs[Y])});
Cs1[Y].addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{ Set(me, Cs1[Y])});
}
function gotoPlace(event:MouseEvent, boing:MovieClip)
{
boing = Plchange;
}
function Set(event:MouseEvent, clip:MovieClip)
{
Plchange.x = clip.x;
Plchange.y = clip.y;
}
I'm getting an error:
TypeError: Error #1010: A term is undefined and has no properties.
at NineMen_fla::MainTimeline/frame1()
where am I going wrong? Please tell me.

First, in gotoPlace you should type Plchange = boing; not vice versa. And second, you have to check if Plchange is assigned a value within function Set(). Like this:
var Plchange:MovieClip=null;
function gotoPlace(event:MouseEvent, boing:MovieClip)
{
Plchange = boing;
}
function Set(event:MouseEvent, clip:MovieClip)
{
if (!Plchange) return; // Plchange is not assigned
Plchange.x = clip.x;
Plchange.y = clip.y;
}

Related

AS3 Error #1009

Please Help me, check this code :
This is output :
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Bechanodroid_LatihanPendahuluan_fla::MainTimeline/tick1()[Bechanodroid_LatihanPendahuluan_fla.MainTimeline::frame3:21]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
this is my code:
import flash.utils.Timer;
import flash.events.TimerEvent;
var countDownInc:Number = 1;
var totalSecs = 10;
var countDownSecs = totalSecs;
timer.text = countDownSecs;
var time:Timer = new Timer(countDownInc*1000);
time.start();
time.addEventListener(TimerEvent.TIMER,tick);
function tick(e:TimerEvent):void{
if(countDownSecs==0){
time.stop();
score+=0;
nextPertanyaan();
countDownSecs=totalSecs;
}else{
countDownSecs=countDownSecs-countDownInc;
timer.text=countDownSecs;
}
this.removeEventListener(Event.ENTER_FRAME, tick);
}
This line:
this.removeEventListener(Event.ENTER_FRAME, tick);
Change it for this:
e.currentTarget.removeEventListener(TimerEvent.TIMER,tick);

AS3 Array items not splicing properly

I'm attempting to make a small game where the user mouses over the circles that fall from the ceiling for points. The circles are added to a container and pushed into an array to hold them, and are removed and spliced when they are mouse-over'd or go off stage.
Everything works fine, until two circles are removed at nearly the same time, whether it be from falling off stage at the same time or mousing over two of them extremely fast. When this happens, the child on stage is removed, but the object is still left in the array, meaning another circle cannot take its place, leaving one less circle spawning every time the issue happens.
Code on main timeline:
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
var ballContainer:Sprite = new Sprite();
addChild(ballContainer);
var maxBalls:uint = 10;
var balls:Array = [];
var ballTypes:Array = [GreenBall];
var ballChances:Array = [800];
var ballVelocities:Array = [1.5];
var ballAccelerations:Array = [1.02];
stage.addEventListener(Event.ENTER_FRAME, onTick);
function onTick(e:Event):void {
while (balls.length < maxBalls){
addBall();
}
}
function addBall():void {
var ballType = ballTypes[0];
var ball = new ballType;
ball.x = Math.ceil(Math.random()*(stage.stageWidth - ball.width));
ball.y = 0 - (ball.height*1.5);
ballContainer.addChild(ball);
balls.push(ball);
}
Code in GreenBall:
import flash.events.Event;
var mainStage = Sprite(root);
var index = mainStage.balls.indexOf(this);
var velocity:Number = mainStage.ballVelocities[0]*randomNumber(0.5, 1.5);
var acceleration:Number = mainStage.ballAccelerations[0];
this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
function onMouseOver(e:MouseEvent):void {
this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
removeBall();
}
this.addEventListener(Event.ENTER_FRAME, onTick);
function onTick(e:Event):void {
this.y += velocity;
velocity = velocity*acceleration;
if (this.y > stage.stageHeight + this.height){
this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
removeBall();
}
}
function removeBall():void {
mainStage.balls.splice(index, 1);//doesn't get spliced if balls are removed too quickly
mainStage.ballContainer.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, onTick);
}
function randomNumber(min:Number, max:Number):Number {
return Math.random()*(max - min) + min;
}
So what's going on? Did I set something up incorrectly? How can I go about fixing this issue?
Any help would be appreciated greatly.
Your logic is flawed - the index should be calculated when the removal occurs. When you remove objects from an array via splice, the index of all the elements after the one you removed is decreased by one.
This means that if you have 10 balls and remove the first, the index value you have for every other ball will be incorrect and you'll be removing the wrong ball from your array on subsequent removals.
Moving the indexOf statement to the removeBall method should solve the issue:
function removeBall():void
{
var index:int = mainStage.balls.indexOf(this);
if(index >= 0)
{
mainStage.balls.splice(index, 1);
mainStage.ballContainer.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, onTick);
}
}
To make it easy on yourself, you could extend Array and make a remove function:
public dynamic class List extends Array
{
public function remove(item:*):void
{
var i:int = indexOf(item);
if(i >= 0) splice(i, 1);
}
}

Actionscript add object to stage from array whats from library

I try import from library to a array and after place objectsd from array to stage.
I'm new in flash and i would make a dress up game for my girlfriend.
import flash.utils.getDefinitionByName;
import flash.display.Bitmap;
import flash.display.MovieClip;
var polc1Ar:Array = new Array();
inditasFu();
function inditasFu(){
behivas();
rakdkiFu();
}
function behivas(){
for (var i=1; i!=3; i++) {
classRef:getDefinitionByName("Symbol" + i.toString()) = new getDefinitionByName("Symbol" + i.toString() as MovieClip)();
var classRef:Symbol1 = new Symbol1();
polc1Ar.push(classRef)
addChild(polc1Ar[0]);
}
}
function rakdkiFu (){
for (var i=0; i!=polc1Ar.length; i++) {
var celTargy:Bitmap = polc1Ar[i] as Bitmap ;
this.addChild(celTargy);
celTargy.x = 25*i;
celTargy.y = 45*i;
}
}
There are lots of Syntax errors in your code. Change all of your code to:
import flash.utils.getDefinitionByName;
import flash.display.Bitmap;
import flash.display.MovieClip;
import flash.display.BitmapData;
var polc1Ar:Array = new Array();
inditasFu();
function inditasFu(){
behivas();
rakdkiFu();
}
function behivas(){
for (var i=1; i!=3; i++) {
var ClassRef:Class = getDefinitionByName("Symbol" + i.toString()) as Class;
var classRef = new ClassRef();
polc1Ar.push(classRef)
addChild(polc1Ar[0]);
}
}
function rakdkiFu (){
for (var i=0; i!=polc1Ar.length; i++) {
var bmpData = new BitmapData(polc1Ar[i].width, polc1Ar[i].height);
bmpData.draw(polc1Ar[i]);
var celTargy:Bitmap = new Bitmap(bmpData);
this.addChild(celTargy);
celTargy.x = 25*i;
celTargy.y = 45*i;
}
}
[1] You cannot execute 'MovieClip as Bitmap' so I used Bitmap.draw() because a MovieClip is an IBitmapDrawable.
[2] You must create a class with getDefinitionByName, you cannot just create the MovieClip directly, unless you were to create very messy code. Basically, the Syntax was wrong for it.
[3] Note that this will only work if you have three Library Clips with Exported Class Names named Symbol1, Symbol2, and Symbol3.

Chain Reaction with Arrays and Distance

I want to make a program in which mines will appear on the screen after a user inputs the number of mines. Then, the user will click on one mine and set off a chain reaction that explodes the nearest two mines.
So far, my code can prompt the user for the number of mines, and then display them. The mines are buttons in which, when clicked, will be removed and an explosion will appear.
However, I am stuck with how I can handle the chain reaction. I am relatively new to coding in AS3 and therefore am stumped with no clue on how to approach this part of my program.
Code:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.events.*;
public class Minefield extends MovieClip
{
var integer:int;
var iField:TextField = new TextField();
var button:iButton = new iButton();
var i:int;
var mines:Array = new Array();
public function Minefield()
{
var explosion:iExplosion = new iExplosion();
iField.type = "input";
iField.height = 18;
iField.x = 460;
iField.y = 275;
iField.border = true;
iField.restrict = "0-9";
iField.maxChars = 2;
stage.focus = iField;
addChild(iField);
addChild(button);
button.x = 450;
button.y = 175;
button.buttonMode = true;
button.addEventListener(MouseEvent.CLICK, UponClick);
}
function AddMines()
{
for (i = 0; i < integer; i++)
{
CreatorOfMine();
mines[i].addEventListener(MouseEvent.CLICK, UponMineClick)
mines[i].buttonMode = true;
}
}
function CreatorOfMine()
{
mines[i] = new Mine();
MineLocation()
}
function MineLocation()
{
mines[i].x = Math.round(Math.random() * 925);
mines[i].y = Math.round(Math.random() * 525);
mines[i].rotation = Math.random() * 360;
addChild(mines[i]);
}
function UponClick(e:MouseEvent)
{
integer = int(iField.text);
RemoverOfChildren();
}
function RemoverOfChildren()
{
removeChild(button);
removeChild(iField);
AddMines();
}
function UponMineClick(event:MouseEvent){
var mineObject:Mine = Mine(event.currentTarget)
var expl:iExplosion = new iExplosion()
expl.x = mineObject.x
expl.y = mineObject.y
expl.rotation = mineObject.rotation
addChild(expl)
removeChild(mineObject)
}
}
}
}
Information you may need/want:
Stage size is 1024 x 600 (px)
Size of mine(s) is 40 x 40 (px)
Size of explosion is 40 x 40 (px)
Looks like a good case for recursion. Pseudo-code:
Function ExplodeMine(mine) {
mine.boooooooooom()
nearest = findNearestUnexplodedMines()
foreach(nextMine in nearest) {
ExplodMine(nextMine);
}
}
Just start ExplodeMine on the first mine that is clicked.

As3 calling array outside of function

I am kind of new to AS3 and programming itself.
I have this issue. I want to be able to use the Pl_array and En_array outside of AfterLoad function, but I always get undefined value. I am writing the code inside the timeline not .as file. Does it matter?
I was trying to return them from function but since it's related to listener i just dont know how to do it also i was trying to make them public.
Here is code on first frame:
import flash.events.MouseEvent;
stop();
Btn_start.addEventListener(MouseEvent.CLICK, onStartClick);
function onStartClick(me:MouseEvent):void{
gotoAndStop("Dictionary");
}
and here is on the second called Dictionary:
import flash.events.Event;
import flash.utils.Timer;
import flash.events.MouseEvent;
stop();
var myTextLoader:URLLoader = new URLLoader();
var Txt_array:Array=new Array(); //tablica wczytanych zwrotów
var Pl_array:Array=new Array();
var En_array:Array=new Array();
var myTimer:Timer = new Timer(1000);
myTextLoader.addEventListener(Event.COMPLETE, onLoaded); //listener na koniec wczytywania pliku tekstowego
function onLoaded(e:Event):void { //funkcja wywoływana przez listener na koniec wczytywania pliku
Txt_array = e.target.data.split(/\n/); //
dispatchEvent(new Event("Load_END"));
}
myTextLoader.load(new URLRequest("Zwroty.txt"));
this.addEventListener("Load_END", AfterLoad); //kod wykonywano po wczytaniu pliku tekstowego
function AfterLoad(e:Event):void{
for each (var s:String in Txt_array){// pętla która rozdziela tekst na polski i angielski
var i:int=0;
En_array[i]=s.substr(0, s.indexOf("-")-1);
Pl_array[i]=s.substr(s.indexOf("-")+2, s.length);
i++;
} //koniec fora
}//koniec funkcji
Begin.addEventListener(MouseEvent.CLICK, test);
function test(e:Event):void{
trace(En_array[1]);
}
//funkcja wyświetlająca string w txt_load
function ShowString (txt_show:String):void{
load_txt.text = txt_show;
}
function ShowOpinion(txt_opinion:String):void{
opinion_txt.text=txt_opinion;
}
function HideOpinion():void{
opinion_txt.text=" ";
}
//funkcja porównująca łańcuchy
function Compare(txt_a:String,txt_b:String):Boolean{
if (txt_a==txt_b){
return true;
}
return false;
}
//up_btn.useHandCursor=true;
//up_btn.addEventListener(MouseEvent.MOUSE_OVER, switch_bg);
//function switch_bg(me:MouseEvent):void{
//var newColor:ColorTransform = me.target.transform.colorTransform;
//newColor.color = 0x1000C6;
//me.target.transform.colorTransform = newColor;
//}
on test function i always get undefined while tracing. I was trying to find solution in Google but couldn't.
This code looks like it should work but if you're trying to access the first element in En_array you need to remember indexing starts at 0, not 1. You might also want to make sure En_array is not empty before reading any of its values. Try this:
if (En_array.length > 0)
trace(En_array[0]);
it sometimes take some seconds to load data first. After loading then read data from array. In timeline, loading must not be at the same frame with the reading process, unless otherwise, explicitly loaded in first line of code then followed by reading.
You have mess in your code. Lets make it simpler:
import flash.events.Event;
import flash.utils.Timer;
import flash.events.MouseEvent;
stop();
var myTextLoader:URLLoader = new URLLoader();
var Txt_array:Array;
var Pl_array:Array;
var En_array:Array;
/* Good practice is to create object when you relay need it */
var myTimer:Timer = new Timer(1000);
myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void {
Txt_array = e.target.data.split(/\n/); // now we create new array
afterLoad(Txt_array);
}
function afterLoad(array):void{ // it is good habit to start function names from small letter
En_array = [];// create arrays
Pl_array = [];
for each (var s:String in array){
En_array.push(s.substr(0, s.indexOf("-")-1));
Pl_array.push(s.substr(s.indexOf("-")+2, s.length));
// push let you add items more efficient and you don't need index
}
}
myTextLoader.load(new URLRequest("Zwroty.txt"));
Now everything should be fine :) Just remember to check if objects (in your case arrays) have elements you want to use (etc. array.lenght>0 or array=null)

Resources