direction problem


hi , how guys? hope al fine

 

here

i have hero has spells 1 of them fire ball , works when  press right ,

shoot fire ball moves right, when shoot fire ball left going left,

but problem when shoot fire ball right(or left) press left

button fire ball moves opposite way

i know problem , why dont know how fix

it here

 

 

if (event.keycode==keyboard.right) {

                gotoandstop(2);
fdir = +1
                dir=1;
    speed = 10
    if (electricalshield.stage)
    addchild(electricalshield)
    if(fball.stage){
  fdir = +1
}
if(!fball.stage){
  fdir = +1
}

            }


            if (event.keycode==keyboard.left) {

                gotoandstop(1);

fdir = -1
                dir=-1;
    speed  = 10
    if (electricalshield.stage){
    addchild(electricalshield)
}
if(fball.stage){
  fdir = -1
}
if(!fball.stage){
  fdir = -1
}

 

 

when press left fire ball going left because of  fdir = -1 when press right button

and fire ball object on stage going +1 trying fix stupid ideas with  

if(fball.stage){

...

}

but useless

 

 

here 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 fball:fireball = new fireball();

var fireballsound:fireballsound = new fireballsound
        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 = 10
  var totalfirespeed = 0
var i:int
var fdir: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(fball.stage){
    fball.x += firespeed * fdir;

    fball.width += 0

    fball.height += 0


    totalfirespeed += 1

}
    if (totalfirespeed == 20)

    {
if(fball.stage){
        movieclip(parent).removechild(fball)

        totalfirespeed = 0
}

    }

}


        public function onkeydown(event:keyboardevent):void {

            if (event.keycode==keyboard.right) {

                gotoandstop(2);
fdir = +1
                dir=1;
    speed = 10
    if (electricalshield.stage)
    addchild(electricalshield)
    if(fball.stage){
  fdir = +1
}
if(!fball.stage){
  fdir = +1
}

            }


            if (event.keycode==keyboard.left) {

                gotoandstop(1);

fdir = -1
                dir=-1;
    speed  = 10
    if (electricalshield.stage){
    addchild(electricalshield)
}
if(fball.stage){
  fdir = -1
}
if(!fball.stage){
  fdir = -1
}

            }

            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)

{

    soundchannel = fireballsound.play()

    fball.name = "fb" + string(i);

   

    fball.width = 20;

    fball.x = x +20

    fball.y = y +10

   

    fireballindex.push(fball);

   

    movieclip(parent).addchild(fball);

    soundchannel = fireballsound.play()

   

    i++;

    trace(i)

    trace(fball.name)

}

  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

the problem have single instance of fireball changes made, identified, affects single fireball.

 

this comes general programming techniques. 1 not have code in 1 class. fireball (and other weapon) can own class. decouple player weapons. can use same concept break out other parts of program they're separate hero class. best practice , common object oriented programming.

 

the quick , dirty other way set kind of flag when fireball fired know it's flying. if player changes direction while it's in air ignore changing direction of fireball. here's example of tracking if fireball flying adding state variable dirty way.

 

http://pastebin.com/5wevesvl

 

i didn't totally reformat code did run general formatter on it. variable fireballflying added class. it's adjusted when fireball made, when dies , checked when user presses left or right.

 

edit:

 

just note, when creating new object, although flash generous , may nothing should use parenthesis @ end of class name. should end completed line of code semicolon. example @ top of class:

 

this:

 

var fireballsound:fireballsound = new fireballsound

var fireballindex:array = new array

var electricity:electricity = new electricity;

var soundchannel:soundchannel = new soundchannel

 

should be:

 

var fireballsound:fireballsound = new fireballsound();

var fireballindex:array = new array();

var electricity:electricity = new electricity();

var soundchannel:soundchannel = new soundchannel();

 

there's more such all-capital variable names reserved "const" or "enum" variable step @ time, you'll on time.



More discussions in ActionScript 3


adobe

Comments

Popular posts from this blog

How to change text Component easybook reloaded *newbee* - Joomla! Forum - community, help and support

PProHeadless.exe has stopped working error when opening projects in Adobe Media Encoder CS6

Preconditions Failed. - Joomla! Forum - community, help and support