How to attach this code to an object?

  • partyjan
  • Student
  • Student
  • partyjan
  • Posts: 69
  • Loc: Norway

Post 3+ Months Ago

I am making a flash game were there is 2 objects that I want to control with different types of key...

for example if I use LEFT, RIGHT, UP and DOWN on the first and A,D,W,S on the second...

My question is therefore:
How do I attach this Keylistener to a Object?



Code: [ Select ]
var KeyListener1:Object = new Object();


Key.addListener(KeyListener1);

KeyListener1.onKeyDown = function () {
    
    
    

    
    if (Key.isDown(Key.LEFT)){
            Xhastighet--;
            break;
            
            if (fisk._xscale > 0)
            fisk._xscale *= -1;
        }
        
            if (Key.isDown(Key.RIGHT)){
            Xhastighet++;
            break;
            
            if (fisk._xscale < 0)
            fisk._xscale *= -1;
        }




    if (Key.isDown(Key.UP)){
            Yhastighet--;
            break;
        }
        
            if (Key.isDown(Key.DOWN)){
            Yhastighet++;
            break;
        }
  1. var KeyListener1:Object = new Object();
  2. Key.addListener(KeyListener1);
  3. KeyListener1.onKeyDown = function () {
  4.     
  5.     
  6.     
  7.     
  8.     if (Key.isDown(Key.LEFT)){
  9.             Xhastighet--;
  10.             break;
  11.             
  12.             if (fisk._xscale > 0)
  13.             fisk._xscale *= -1;
  14.         }
  15.         
  16.             if (Key.isDown(Key.RIGHT)){
  17.             Xhastighet++;
  18.             break;
  19.             
  20.             if (fisk._xscale < 0)
  21.             fisk._xscale *= -1;
  22.         }
  23.     if (Key.isDown(Key.UP)){
  24.             Yhastighet--;
  25.             break;
  26.         }
  27.         
  28.             if (Key.isDown(Key.DOWN)){
  29.             Yhastighet++;
  30.             break;
  31.         }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • dann_the_van
  • Novice
  • Novice
  • dann_the_van
  • Posts: 23
  • Loc: South Wales, UK

Post 3+ Months Ago

i'm not too sure on this, as i don't use keylisteners!?

but can this be done via putting all this code into a seperate actionscript file (.as), but make a duplicate with the alternatives.
you then grab the relevant scriptfile via the use of the movieclip's name!? For example,

car1 = car1.as
car2 = car2.as

you can use the #attach statement to do this!?

Or alternatively, create duplicate movieclips with the variation and attach the movieclip to the stage
  • IceCold
  • Guru
  • Guru
  • User avatar
  • Posts: 1254
  • Loc: Ro

Post 3+ Months Ago

you can add more key listeners to the same object.
But you don't need 2, just add your code for the keys A, S, D, W in the same keylistener

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 6 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.