• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

move the object left and right

Participant ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

hi < how are you all? i hope you all fine...

iam now trying to make more spells for the hero and this spell is fireball

and i want to move right when the hero is on the right also left when the hero on the left

i tried with this code when you press w it should move 10 or 20 px then it should stop

but idont this step "then it should stop"  because of this error also the fireball does,t move

as i want

TypeError: Error #1010: A term is undefined and has no properties.

at HERO/onEnterFrame()

here is the code

HERO class

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 firespeed:int = 50
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;
   if(fireballindex.stage){
    fireballindex.x += 1
   }

        }

        public function onKeyDown(event:KeyboardEvent):void {

            if (event.keyCode==Keyboard.RIGHT) {

                gotoAndStop(2);

                dir=1;
    speed = 10
    if (ELECTRICALSHIELD.stage)
    addChild(ELECTRICALSHIELD)
   

            }


            if (event.keyCode==Keyboard.LEFT) {

                gotoAndStop(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())

addChild(fireballindex)
fireballindex.width = 50
fireballindex.x += firespeed * dir

i++
trace(i)
   }
  if (event.keyCode == Keyboard.E) {
MovieClip(parent).aa.x = x +100
  }
}

        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)
            }

        }

    }

}

thank you

TOPICS
ActionScript

Views

2.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 27, 2012 Aug 27, 2012

I would not use " i " to target the fireball object.  If fireballs happen to be getting removed at any point, that value of i will be erroneous.  The fireball you add might not be the one at fireballindex.

If all the code is happening in the same vicinity, then I would declare the instance by a var name and use the name...

var fBall:fireball = new fireball();

fBall.name = "fb"+String(i);

fBall.width = 50;
fBall.x += firespeed * dir;

fireballindex.push(fBall);

addChild(fBall);

i++;

Later on if you need to

...

Votes

Translate

Translate
LEGEND ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

Use the trace command to find out which object in the onEneterFrame function is causing the error...

        public function onEnterFrame(event:Event):void {

            trace(x, dir, speed, i, fireballindex, fireballindex.stage);

            x +=dir*speed;
            if(fireballindex.stage){
                   fireballindex.x += 1
             }

        }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

i did that , nothing happend

i copied your trace command under the onEnterFrame function

thank you for your replay

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

If nothing happened, then you need to see why your onEnterFrame function is not being called.  Trace thru the processing route of your code to see where things stop working as you know they should.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

i know the problem from where , i always check the code after every small detail

it is  from here

if(fireballindex.stage){

fireballindex.x += 1

}

if i remove these lines the code is gonna work

thank you again for your replay and sorry because i late in the replay

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

If you checked and know what's wrong with that code, then you should be able to fix it.  If you don't know what's wrong with that code, you need to use the trace command to see which variable/object is causing the error.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

i will try that , but my way to use this spell is wrong because of that i asked you , because iam sure that you have better way to move the fire ball object to the left when the hero is left and to the right when the hero is right and remove the fireball object

from the stage after 30 px from moving

here

if (event.keyCode == Keyboard.W) { 


fireballindex.push(new fireball())

addChild(fireballindex)
fireballindex.width = 50
fireballindex.x += firespeed * dir

i++
trace(i)
}

i just tried with the direction idea to do the move but as you see it doesn,t move like i want also i get error , so can you give me better way to do this move please

thank you again  for your replay

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 27, 2012 Aug 27, 2012

Copy link to clipboard

Copied

I would not use " i " to target the fireball object.  If fireballs happen to be getting removed at any point, that value of i will be erroneous.  The fireball you add might not be the one at fireballindex.

If all the code is happening in the same vicinity, then I would declare the instance by a var name and use the name...

var fBall:fireball = new fireball();

fBall.name = "fb"+String(i);

fBall.width = 50;
fBall.x += firespeed * dir;

fireballindex.push(fBall);

addChild(fBall);

i++;

Later on if you need to target that fireball you can use the array or you can use its name.

If you wanted to still use the non-var approach (I see no reason to) then instead of targeting

      fireballindex

I would target the last item added to the array which is definitiely going to be

      fireballindex[fireballindex.length-1])

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 28, 2012 Aug 28, 2012

Copy link to clipboard

Copied

LATEST

thank you , i will try to do that

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines