Skip navigation
AntonioOrozcoZaid
Currently Being Moderated

How do you call two actionscript files in another as3

Apr 21, 2012 6:09 PM

My problem is that i what to call two actionscript files in another one, but cant do it.

 

This is are the two codes i want to play at the same time in my scene:

 

 

Code 1

/**
 * Inventory System Manager
 * ---------------------
 * DATE: 07/24/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package  
{
    import flash.display.Sprite;
    
    import com.freeactionscript.inventorySystem.InventorySystem;
    
    public class Main extends Sprite
    {        
        private var _inventorySystem:InventorySystem;
        
        public function Main() 
        {            
            //InventorySystem(reference to stage in the fla)
            _inventorySystem = new InventorySystem(this);
        }        
    }
}

 

 

Code 2

 

/**
 * Player Movement - 8-way keyboard
 * ---------------------
 * VERSION: 1.0
 * DATE: 9/23/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package  
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    
    public class Main extends MovieClip
    {        
        // player
        private var _player:MovieClip;
        
        
        // player settings
        private var _playerSpeed:Number = 4;
        
        // movement flags
        private var _movingUp:Boolean = false;
        private var _movingDown:Boolean = false;
        private var _movingLeft:Boolean = false;
        private var _movingRight:Boolean = false;
        
        /**
         * Constructor
         */
        public function Main() 
        {
            createPlayer();
            
            // add listeners
            stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
            stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);
        }
        
        /**
         * Creates player
         */
        private function createPlayer():void
        {
            _player = new Player();
            _player.x = stage.stageWidth / 2;
            _player.y = stage.stageHeight / 2;
            stage.addChild(_player);
        }
        
        /**
         * EnterFrame Handlers
         */
        private function enterFrameHandler(event:Event):void
        {
            // Move up, down, left, or right
            if ( _movingLeft && !_movingRight )
            {
                _player.x -= _playerSpeed;
                _player.rotation = 270;
            }
            if ( _movingRight && !_movingLeft )
            {
                _player.x += _playerSpeed;
                _player.rotation = 90;
            }
            if ( _movingUp && !_movingDown )
            {
                _player.y -= _playerSpeed;
                _player.rotation = 0;
            }
            if ( _movingDown && !_movingUp )
            {
                _player.y += _playerSpeed;
                _player.rotation = 180;
            }
            
            // Move diagonally
            if ( _movingLeft && _movingUp && !_movingRight && !_movingDown )
            {
                _player.rotation = 315;
            }
            if ( _movingRight && _movingUp && !_movingLeft && !_movingDown )
            {
                _player.rotation = 45;
            }
            if ( _movingLeft && _movingDown && !_movingRight && !_movingUp )
            {
                _player.rotation = 225;
            }
            if ( _movingRight && _movingDown && !_movingLeft && !_movingUp )
            {
                _player.rotation = 135;
            }
            
            //i will do
            if (_player.x > 550)
            {
                _player.x = 550;
                _player.x = +_player.x;
            }
            else if (_player.x < 50)
            {
                _player.x = 50;
                _player.x = +_player.x;
            }

            if (_player.y > 350)
            {
                _player.y = 350;
                _player.y = +_player.y;
            }
            else if (_player.y < 50)
            {
                _player.y = 50;
                _player.y = +_player.y;
            }
            trace ("Valor en x: " + _player.x);
            trace ("Valor en y: " + _player.y); 
        }
        
    
        /**
         * Key Press Handlers
         */
        public function myOnPress(event:KeyboardEvent):void
        {
            switch( event.keyCode )
            {
                case Keyboard.UP:
                    _movingUp = true;
                    break;
                    
                case Keyboard.DOWN:
                    _movingDown = true;
                    break;
                    
                case Keyboard.LEFT:
                    _movingLeft = true;
                    break;
                    
                case Keyboard.RIGHT:
                    _movingRight = true;
                    break;
            }
        }
        
        /**
         * Key Release Handlers
         */
        public function myOnRelease(event:KeyboardEvent):void
        {
            switch( event.keyCode )
            {
                case Keyboard.UP:
                    _movingUp = false;
                    break;
                    
                case Keyboard.DOWN:
                    _movingDown = false;
                    break;
                    
                case Keyboard.LEFT:
                    _movingLeft = false;
                    break;
                    
                case Keyboard.RIGHT:
                    _movingRight = false;
                    break;
            }
        }
    }
    
}

 

 

Hope you help me =)

 
Replies
  • Currently Being Moderated
    Apr 21, 2012 6:20 PM   in reply to AntonioOrozcoZaid

    You need to have a reference to an instance of one class in the other class or the other way round in order to communicate between the two. Alternatively you can call a static function of one class from an instance of the other class.

     

    Anyway, I suggest reading about object-oriented programming first (can be even a Java book) to understand how things work. Such basics really help and it's never shame to read it.

     

    Good luck

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 21, 2012 6:23 PM   in reply to AntonioOrozcoZaid

    You do not want to have two classes named the same ":Main"  It looks like if you name one Inventory and the other Player, you might be in a better position to have them both called into your main file.  In your main file you could then acquire them using...

     

    import Inventory;

    import Player;

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 22, 2012 4:18 AM   in reply to AntonioOrozcoZaid

    You also need to create instances of those classes.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points