Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-24-2011, 03:03 AM   #1
LordFrz
Registered User
 
Join Date: Apr 2011
Posts: 10
Question [AS3] Adding Public Class MC to Stage

I am trying to add a movieclip to my main timeline, that is linked to a class.

How might I go about this?.

Class Code:

Code:
package com.Player 
{
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	public class Move extends MovieClip 
		{
		
public var speed:Number = 0.3;
public var rotateSpeed:Number = 3;
public var vx:Number = 0;
public var vy:Number = 0;
public var friction = 0.98;
public var maxSpeed = 5.9;
public var Gravity = 9.81
public var Jump = 40;
public var Run = 10;
public var upPressed:Boolean = false;
public var downPressed:Boolean = false;
public var leftPressed:Boolean = false;
public var rightPressed:Boolean = false;
		
		
		public function Move () : void
		{
		this.addEventListener(Event.ENTER_FRAME, MoveInDirectionOfKey);
		stage.addEventListener(KeyboardEvent.KEY_DOWN, SetKeyPressed);
		stage.addEventListener(KeyboardEvent.KEY_UP, UnsetKeyPressed);
		}
		
public function MoveInDirectionOfKey(event:Event):void
{
	if (upPressed)
	{	
		y -= Jump;
		} else 
			{
			    //Gravity
				y += Gravity;
			}

	if (downPressed)
	{}
	if (leftPressed)
	{x-= Run;}
	if (rightPressed)
	{x+= Run;}
	
	
}

public function SetKeyPressed(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.UP:
		{upPressed = true;break;}
		case Keyboard.DOWN:
		{downPressed = true;break;}
		case Keyboard.LEFT:
		{leftPressed = true;break;}
		case Keyboard.RIGHT:
		{rightPressed = true;break;}
		
		case Keyboard.W:
		{upPressed = true;break;}
		case Keyboard.S:
		{downPressed = true;break;}
		case Keyboard.A:
		{leftPressed = true;break;}
		case Keyboard.D:
		{rightPressed = true;break;}
	}
}

public function UnsetKeyPressed(event:KeyboardEvent):void
{
	switch (event.keyCode)
	{
		case Keyboard.UP:
		{upPressed = false;break;}
		case Keyboard.DOWN:
		{downPressed = false;break;}
		case Keyboard.LEFT:
		{leftPressed = false;break;}
		case Keyboard.RIGHT:
		{rightPressed = false;break;}
		
		case Keyboard.W:
		{upPressed = false;break;}
		case Keyboard.S:
		{downPressed = false;break;}
		case Keyboard.A:
		{leftPressed = false;break;}
		case Keyboard.D:
		{rightPressed = false;break;}
	}
}
		
public function degreesToRadians(degrees:Number) : Number
		{
			return degrees * Math.PI / 180;
		}
		

	}
	
}

Main Code - *Does not work

Code:
import flash.display.MovieClip;
import com.Player.Move;







var Player:MovieClip;
Player = new com.Player.Move(); // What do I do for this to work?

addChild(Player)

Player.x = 250;
Player.y = 200;

this.addEventListener(Event.ENTER_FRAME, HitTest);
function HitTest(e:Event):void
{

}

Works fine if I just drag the MovieClip from library.
LordFrz is offline   Reply With Quote
Old 12-24-2011, 10:19 AM   #2
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

What error happens? If any.
Make sure the com folder is in a place that is under the folder that the fla file is in.
Code:
root - 
   com -
   fla

or

 root -
  game folder - fla
  com -
And you should be able to just do
ActionScript Code:
import flash.display.MovieClip; import com.Player.Move; var player:MovieClip;//also don't use  uppercase for player variable. player = new Move();
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
tadster is offline   Reply With Quote
Old 12-24-2011, 05:42 PM   #3
LordFrz
Registered User
 
Join Date: Apr 2011
Posts: 10
Default

The error I get after doing what you told me.
Quote:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.Game.Player::Move()
at SideScroll_fla::MainTimeline/frame1()
File structure is

root - Game Fla
com-
Game-*Just added this, but have changed all references.
Player-Move
LordFrz is offline   Reply With Quote
Old 12-24-2011, 06:08 PM   #4
LordFrz
Registered User
 
Join Date: Apr 2011
Posts: 10
Default

I tried creating a Main class and referencing it to the main game. Here is code and error I get now.

Code:
package com.Game {
	import com.Game.Player.Move;
	import flash.display.MovieClip;
	public class Main extends MovieClip{

		public function Main() 
		{
			trace("Add Player");
			var player:MovieClip;
			player = new Move();
			addChild(player);
			player.x = 50;
			player.y = 50;
		}

	}
	
}



Quote:
Add Player
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.Game.Player::Move()
at com.Game::Main()

Last edited by LordFrz; 12-24-2011 at 06:23 PM. Reason: Modded Code
LordFrz is offline   Reply With Quote
Old 12-24-2011, 08:28 PM   #5
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

ok, so it seems your problem is not the file structure.

What is happening is the stage is null during the Move constructor.
Just a few changes in Moves code are needed.
ActionScript Code:
public function Move () : void         {              if(stage) { ini(null); } else { this.addEventListener(Event.ADDED_TO_STAGE, ini); }         } private function ini(e:Event):void {   this.removeEventListener(Event.ADDED_TO_STAGE, ini);   this.addEventListener(Event.ENTER_FRAME, MoveInDirectionOfKey);   stage.addEventListener(KeyboardEvent.KEY_DOWN, SetKeyPressed);   stage.addEventListener(KeyboardEvent.KEY_UP, UnsetKeyPressed);   }
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
tadster is offline   Reply With Quote
Old 12-24-2011, 10:09 PM   #6
LordFrz
Registered User
 
Join Date: Apr 2011
Posts: 10
Default

Thanks that did the trick!
LordFrz is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:37 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.