hi , how are you,i hope you all fine
i have problem with arrays with objects , i want to create an fireball after clicking
i tried to do one fireball(only fireballobject i dont wanna move it at this time , i just want it on the stage) and it is so easy with this addChild(new fireball()) but because i always get errors if i dont check the object "if (objectname.stage){}" before adding or removing it from stage i couldn,t do that ,
now iam trying now with arrays to display one object
here
if (event.keyCode==Keyboard.W) {
fireballindex.push(new fireball())
if(fireballindex[0].stage)
addChild(fireballindex[0])
trace("if statement works")
}
i got this message " if statement works" afte clicking w but the object not on the stage??!!
in otherwise
it works
if (event.keyCode==Keyboard.W) {
addChild(new fireball())
}
but with errors #2025 or 1009 (i dont remembe) but iam sure , the solution with if(objectname.stage)
thank you
there is no problem with code , it works very well , the problem from here only ( iam sure about that ) i always check it
but here
i will bring the fire ball and hero classes
hero
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.sensors.Accelerometer;
import flash.media.Sound;
import flash.media.SoundChannel;
public class HERO extends MovieClip {
var NIGHTJUNGLE:nightjungle = new nightjungle
var ELECTRICALSHIELD:electricalshield=new electricalshield ;
var speed:int=3;
var fireballindex:Array = new Array
var dir:int;
var ELECTRICITY:electricity = new electricity;
var SOUNDCHANNEL:SoundChannel = new SoundChannel
var i:int
public function HERO() {
addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
}
public function onAddedToStage(event:Event):void {
addEventListener(Event.ENTER_FRAME,onEnterFrame );
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
SOUNDCHANNEL = NIGHTJUNGLE.play(1,1000)
gotoAndStop(4);
}
public function onEnterFrame(event:Event):void {
x+=dir*speed;
}
public function onKeyDown(event:KeyboardEvent):void {
if (event.keyCode==Keyboard.RIGHT) {
gotoAndStop(2);
dir=1;
speed = 10
if (ELECTRICALSHIELD.stage)
addChild(ELECTRICALSHIELD)
fireball.dir = +1
}
if (event.keyCode==Keyboard.LEFT) {
gotoAndStop(1);
fireball.dir = -1
dir=-1;
speed = 10
if (ELECTRICALSHIELD.stage)
addChild(ELECTRICALSHIELD)
}
if (event.keyCode==Keyboard.UP) {
gotoAndStop(1);
y=10;
}
if (event.keyCode==Keyboard.Q) {
speed=30;
addChild(ELECTRICALSHIELD);
SOUNDCHANNEL = ELECTRICITY.play()
}
if (event.keyCode==Keyboard.W) {
fireballindex.push(new fireball())
if(fireballindex[0].stage)
addChild(fireballindex[0])
trace("if statement work")
}
}
public function onKeyUp(event:KeyboardEvent):void {
if (event.keyCode==Keyboard.Q) {
if (ELECTRICALSHIELD.stage) {
removeChild(ELECTRICALSHIELD);
if (SOUNDCHANNEL != null)
speed=0;
SOUNDCHANNEL = ELECTRICITY.play(1,1)
}
}
if (event.keyCode==Keyboard.W) {
}
if (event.keyCode==Keyboard.LEFT) {
speed = 0
gotoAndStop(3);
if (ELECTRICALSHIELD.stage)
addChild(ELECTRICALSHIELD)
} else if (event.keyCode == Keyboard.RIGHT) {
speed =0
gotoAndStop(4);
if (ELECTRICALSHIELD.stage)
addChild(ELECTRICALSHIELD)
}
}
}
}
fireball
package {
import flash.display.MovieClip;
import flash.events.Event;
public class fireball extends MovieClip {
public static var speed1:int
public static var dir:int
var vx:int
public function fireball() {
addEventListener(Event.ADDED_TO_STAGE,onAddedToStage)
}
public function onAddedToStage(event:Event):void
{
y = 10
x = 2
vx = 0
addEventListener(Event.ENTER_FRAME,onEnterFrame )
addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage)
}
public function onRemovedFromStage(event:Event):void{
removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage)
removeEventListener (Event.ENTER_FRAME,onEnterFrame )
removeEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage)
trace("fire ball removed")
}
public function onEnterFrame (event:Event)
{
x+=dir*speed1;
if (dir >= 0){
speed1++
}
if (dir <= 0){
speed1--
}
if(this.stage){
if (speed1 == -3 || speed1 == 3 ) {
removeChild(this)
}
}
}
}
}
and there are some classes main and bat1etc...
butall the problem from these two classes
thank you for your replay
there are no errors , but there are some objects dont work
here
i have hero and whenyou click w he should has fire ball on his hand every time i click W and that is so easy by doing
this
addChild(new fireball()) // in click event place
but with my project i should do this before adding or removing any child or i will get this error
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller...
i ,ve read book about games programming and i,ve never heard about this error but with my project i should do if statement and
i cant do that because i dont know the object name ( and you know that already)
then you gave me the arrays idea and iam trying to do th same thing with arrays , chicking object (objectname.stage) then display it
and i tried to do that but the object didn,t display with this code
var fireballindex:array = new array
....
...
if (event.keyCode==Keyboard.W) {
fireballindex.push(new fireball())
if(fireballindex[0].stage)
addChild(fireballindex[0])
trace("if statement works")
}
here is the problem from thiscode , i,ve never used arrays with objects so the problem from here
iam sure there is wrong in this statement
thank you again dr.murphy for your replay
Before you can test if the object has a non-null stage property it needs to be added to the stage. So based on the code you just showed, the object will never get added as a child.
You will not get the 2025 error in trying to use addChild. You can get it trying to removeChild if it is not a child.
If you trace the fireballindex[0].stage before the line where you test it, you will see that it is null, and that will cause it to fail getting in to add the child...
if (event.keyCode==Keyboard.W) {
fireballindex.push(new fireball())
trace(fireballindex[0].stage)
if(fireballindex[0].stage)
addChild(fireballindex[0]) // this needs to happen first
trace("if statement works")
}
lol , yeah i forgot that
..also i,e checked the code and i findthe error not from the hero class but from the fireball class
now i fix them all but the problem still
this command
addChild(fireballindex[0]) doesn,t display object like addChild(new fireball())
it does nothing ! idont know why??
i also tried this
if (event.keyCode==Keyboard.W) {
fireballindex.push(new fireball())
addChild(fireballindex[0])
nothing happend ? maybe addChild doesn,t work with arrays
var fireballindex:Array = new Array();
fireballindex.push(new fireball())
addChild(fireballindex[0])
does work. You can confirm this yourself by opening a new file and testing just those lines with a fireball class object in the library. Whatever the problem is, it lies elsewhere. Maybe you don't add the object that contains the fireball? Maybe you are adding the fireball somewhere that it cannot be seen?
You need to learn how to make better use of the trace function and test things for yourself to see how they are working or not working. Go line by line thru the expected processing with trace statements until you do not see an expected result - solve things as they arise in that sequence.
This lines:
if(fireballindex[0].stage)
addChild(fireballindex[0])
will never work because the latest object's stage property is null. Object should be already on stage in order for this conditional to return true.
Code should be (note exlamation mark):
if(!fireballindex[0].stage)
addChild(fireballindex[0])
Which is similar to
if(fireballindex[0].stage != null)
addChild(fireballindex[0])
Although it is not related to the discussion directly, here is coding practices suggestion.
You should use switch...case instead of if...else whenever possible. The thing is that switch...case is indexed at compilation while if...else is not. This means that if...else goes through all the conditions until it hits validation to true. switch...case goes to the appropriate case right away. This translates into a much faster performance.
With that said, your HERO class should look something like the example below. Note onKeyDown case Keyboard.W that addresses fireball instantiation - if you are planning to use several fireball instances - suggested code is the way to go:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.sensors.Accelerometer;
import flash.media.Sound;
import flash.media.SoundChannel;
public class HERO extends MovieClip
{
private var NIGHTJUNGLE:nightjungle = new nightjungle();
private var ELECTRICALSHIELD:electricalshield = new electricalshield();
private var speed:int = 3;
private var fireballindex:Array = new Array();
private var dir:int = 0;
private var ELECTRICITY:electricity = new electricity();
private var SOUNDCHANNEL:SoundChannel = new SoundChannel();
private var i:int = 0;
public function HERO()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
public function onAddedToStage(event:Event):void
{
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
SOUNDCHANNEL = NIGHTJUNGLE.play(1, 1000)
gotoAndStop(4);
}
public function onEnterFrame(event:Event):void
{
x += dir * speed;
}
public function onKeyDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.RIGHT:
gotoAndStop(2);
fireball.dir = 1;
dir = 1;
speed = 10;
if (ELECTRICALSHIELD.stage)
addChild(ELECTRICALSHIELD);
break;
case Keyboard.LEFT:
gotoAndStop(1);
fireball.dir = -1;
dir = -1;
speed = 10;
if (ELECTRICALSHIELD.stage)
addChild(ELECTRICALSHIELD);
break;
case Keyboard.UP:
gotoAndStop(1);
y = 10;
break;
case Keyboard.Q:
speed = 30;
addChild(ELECTRICALSHIELD);
SOUNDCHANNEL = ELECTRICITY.play();
break;
case Keyboard.W:
var fBall:fireball = new fireball();
fireballindex.push(fBall);
addChild(fBall);
break;
}
}
public function onKeyUp(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.Q:
if (ELECTRICALSHIELD.stage)
{
removeChild(ELECTRICALSHIELD);
if (SOUNDCHANNEL != null)
speed = 0;
SOUNDCHANNEL = ELECTRICITY.play(1, 1);
}
break;
case Keyboard.W:
break;
case Keyboard.LEFT:
speed = 0
gotoAndStop(3);
if (ELECTRICALSHIELD.stage)
addChild(ELECTRICALSHIELD);
break;
case Keyboard.RIGHT:
speed = 0
gotoAndStop(4);
if (ELECTRICALSHIELD.stage)
addChild(ELECTRICALSHIELD);
break;
}
}
}
}
I think error 2025 originates in fireball class on lines:
if (this.stage)
{
if (speed1 == -3 || speed1 == 3)
{
removeChild(this);
}
}
What your code does is it attempts to remove fireball instance from itself (from its display list). Fireball IS NOT on its own display list.
To remedy this you should do this:
if (this.stage)
{
if (speed1 == -3 || speed1 == 3)
{
parent.removeChild(this);
}
}
I feel the very approach of removing instance from display list from within the instance is faulty. It may introduce challenges in the parent. How does parent know that the object is removed from its display list?
I believe you should rethink this approach.
lol ,yeah
also there are some mistakes in the library but i fixed them all now , now iam trying to move the fire ball left and right and i did some code for that bu it works only the direction
am gonna open now new discuss for that problem
sorry brother i used my limit in helpful answer and i cant give you points for your help
sorry again
North America
Europe, Middle East and Africa
Asia Pacific