Questions about as 3

few questions about platform game in as3

Page 1 of 1

11 Replies - 836 Views - Last Post: 30 November 2010 - 01:32 PM Rate Topic: -----

#1 Aivenn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 10

Questions about as 3

Posted 24 November 2010 - 08:20 AM

Hi all,

I'm making a game using Cs5 flash and action script 3, this game is basic platforme which character can jump run die, there will be no enemies only death traps and pillars to jump from the whole point will be to get from one end to the next while collecting coins as fast as you can not timed but with a timer that keeps track of how long it takes you. I also want to keep track of the scores of each person who finishes or dies with their initials by their score like an old arcade game.

Alright, my questions are probably pretty noobish but please deal with me for a bit. My first question is about flash, do I have to use multiple frames for a platform game. I've read and seen a bunch of tutorials which have anywhere from 3 to 10 different frames with several layers. What I want to do is make a flash game with 1 layer and frame using the action script to call on the functions needed or if possible write 90% of everything in action script and only have the levels in the frames. Is this possible and how would I go about doing it.

Alright second, I'm trying to make a map editor using the flash IDE so my level designer can make the map while I write the code for the variables such as floor and wall. I understand that I must write an .as for each of those like Floor.as and Wall.as(unless I'm incorrect) and then reference them in my Game.as to call on them or check the level for them.

I want to avoid writing a single class as much as possible I did that in a tutorial and it ended up being 500 lines of craziness, it was hard pinpointing the information I needed.

Thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Questions about as 3

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: Questions about as 3

Posted 24 November 2010 - 09:45 AM

The multiple frame can be seen as multiple games states if you like, one frame could hold the menu for you to start your game and then when you clicc the button to start the game moves to the next frame which uses your .as files.

You can create any movieclip and then make it accessible from your actionscript code by first making it a movieclip and then setting its linkage setting to whatever you want to be called inside your .as files.

var myMovieClip:ClipInLibrary = new ClipInLibrary();
addChild(myMovieClip);



Where ClipInLibrary is the instance name of the object you want to access from your library.

You can break up your code into as many files as you want, for instance you could create a player class that defines a player object and its member variables and then also contains all the logic for that object then you could create an instance of this object in your game class file.
Was This Post Helpful? 0
  • +
  • -

#3 Aivenn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 10

Re: Questions about as 3

Posted 24 November 2010 - 10:07 AM

Alright I just wrote this real fast, how would I implement this into my game.as I'm still unsure about a few things, Should my variables be public? As for gravity when I introduce it shouldn't it be in the Main.as file? any help cleaning this up would be nice as well I've not yet got a hold of coding style.

package{
	
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	
	public class Spudnick extends MovieClip{
		
		var rightKeyIsDown:Boolean=false;
		var leftKeyIsDown:Boolean=false;
		var spaceKeyIsDown:Boolean=false;
		
		stage.addEventListener(KeyboardEvent.KEY_DOWN, PressKey);
		stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseKey);
				
		public function Spudnick( startX:Number, startY:Number ){
					
					x = startX;
					y = startY;
		}
		function PressKey(event:KeyboardEvent):void{
			if(event.keyCode == Keyboard.RIGHT){
				rightKeyIsDown = true;
			}
			if(event.keyCode == Keyboard.LEFT){
				leftKeyIsDown = true;
			}
			if(event.keyCode == Keyboard.SPACE){
				spaceKeyIsDown = true;
			}
		}
		function ReleaseKey(event:KeyboardEvent):void{
			if(event.keyCode == Keyboard.RIGHT){
				rightKeyIsDown = false;
			}
			if(event.keyCode == Keyboard.LEFT){
				leftKeyIsDown = false;
			}
			if(event.keyCode == Keyboard.SPACE){
				spaceKeyIsDown = false;
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: Questions about as 3

Posted 24 November 2010 - 10:47 AM

You declare a variable as public if you want it to be accessible outside of the class, I don't think the variables you are using here need to be so they are fine how they are.

Your player should have an update function that you could pass the gravity to, but to be honest gravity is going to be constant so you could just set it in player for simplicity and then if something is not affected by gravity then just don't implement it.
Was This Post Helpful? 0
  • +
  • -

#5 Aivenn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 10

Re: Questions about as 3

Posted 28 November 2010 - 04:27 PM

Thanks for your help for the past few days really did a lot.

I have a few new questions tho. Alright, I now know that you can call to stuff by using something like
var movement:Movement = new Movement;
var gravity = movement.gravity;



Is this the correct way of doing it or is there a better way?

Also I seem to be running to this problem a lot:
addEventListener(Event.ENTER_FRAME, enterframe);
stage.EventListener(KeyboardEvent.KEY_DOWN, pressed); // want to place these into the Movement.as
stage.EventListener(KeyboardEvent.KEY_UP, released);  //



See what I wanted to do is place the pressed and released into the movement.as then call to them from say the hero.as then do a get_hero(); event from the ENTER_FRAME. I get the character on the stage where I place him from addChild(hero); but theres no movement. could you tell me what I'm doing wrong or if its just not possible?

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: Questions about as 3

Posted 29 November 2010 - 08:48 AM

I find it slightly odd that you have used a separate class for movement, I would have thought that a player would just need to know its movement without it being in a separate class.

You could make this so that your player class has boolean values such as move left and move right and then in its update function (or a separate function) it can have something like so:

pseudo code
if(move right)
{
   velocity.x += 1.0;
}
else if(move left)
{
   velocity.x -= 1.0;
}
else
{
   velocity.x = 0;
}

position.x += velocity.x;



In your game class where the main game logic happens you can use an event listener which simply sets the players movement flags to true or false respectively. This may need some tweaking or it may be just right. Hope that helps some.
Was This Post Helpful? 0
  • +
  • -

#7 Aivenn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 10

Re: Questions about as 3

Posted 29 November 2010 - 08:59 AM

Alright so it would be simpler to just remove the Movement.as?

I was just trying to break things down so I'd have a personal reference engine to go on, this would never be an actual game.
Alright, then I'll use Main, hero, and collision. these 3 should be enough until I add enemies and so on.

I wanted to do a Update_hero() function I found in a tutorial.
public var get_hero():void
{
  y_speed += gravity;

  if(leftKey)
  {
     x_speed = walkspeed;
  }
  etc...
}



then in my
private function getframe(e:Event):void {
hero.get_hero();
}



I've seen this done in a tutorial but every time I do it, I cant get the character to move. However by removing the Movement.as this has probably become a lot simpler.
Was This Post Helpful? 0
  • +
  • -

#8 Aivenn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 10

Re: Questions about as 3

Posted 29 November 2010 - 09:39 AM

Alright, I think I must be really tired because in my last post I think I just reposted the information you gave me but in another way..

Well before I get more confused or start loosing my mind here is my Main.as
package  
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	/**
	 * ...
	 * @author Travis Allen
	 */
	public class Main extends MovieClip
	{
		public const gravity:int = 1;
		public const walkspeed:int = 4;
		
		public const max_speed:int = 8;
		public const jumpspeed:int = 10;
		
		private var hero:Hero = new Hero;
		private var get_hero = hero.get_hero;
		
		public var leftKey:Boolean;
		public var rightKey:Boolean;
		public var spaceKey:Boolean;
		
		public function Main() 
		{
			addChild(hero);
			addEventListener(Event.ENTER_FRAME, getframe);
			stage.addEventListener(KeyboardEvent.KEY_UP, pressed);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, released);
		}
		
		private function pressed(e:KeyboardEvent)
		{
			if (e.keyCode == Keyboard.LEFT)
			{
				leftKey = true;
			}
			if (e.keyCode == Keyboard.RIGHT)
			{
				rightKey = true;
			}
			if (e.keyCode == Keyboard.SPACE)
			{
				spaceKey = true;
			}
		}
		
		private function released(e:KeyboardEvent)
		{
			if (e.keyCode == Keyboard.LEFT)
			{
				leftKey = false;
			}
			if (e.keyCode == Keyboard.RIGHT)
			{
				rightKey = false;
			}
			if (e.keyCode == Keyboard.SPACE)
			{
				spaceKey = false;
			}
		}
		
		private function getframe(e:Event):void 
		{
			get_hero();
		}
		
	}

}



I hope this helps a bit more. Alright, I can trace("working?"); in the get_hero() function and it works fine but if I add key movements the object does nothing. I've also tried x_speed += 1; just to see if I could get the object to continually move down 1 but nothing happens. I'm also finding it hard to call to the constants of Main from hero keep getting an error when I main:Main = new Main; the whole reason I made constants was so I wouldn't have to write them every where and I could just change a single integer.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#9 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: Questions about as 3

Posted 29 November 2010 - 04:15 PM

You have misunderstood the concept of const. You use const when you don't want that variable to ever change, hence constant. You won't be able to change the value of a const variable at anytime without the compiler giving you an error which is exactly what you want anyway.

You need to use static variables for what you would like to do, if you declare a variable as static then it will exist for the lifetime of the program and you can change its value. Plus changing its value will make it change across every instance of the object it is declared in.

This post has been edited by stayscrisp: 29 November 2010 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#10 Aivenn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 10

Re: Questions about as 3

Posted 29 November 2010 - 05:12 PM

Ok I'm confused. Gravity is supposed to be constant right? The integers I have as constants I don't want them to change, the reason I want them set up the way they are is so If I need to modify gravity or walkspeed I only have to change the constant and never have to worry about going through all my code.

Alright here is the error I get when I try to call to Main for the constant.

Error: Error #2136: The SWF file file:///C|/Users/Aivenn/Desktop/Spudnick/Spudnick.swf contains invalid data.
at Hero()
at Spudnick() //Main


I believe the error I'm getting is because hero:Hero = new Hero; is a call to the hero class so its having an error because Main can't call main:Main = new Main inside the hero class.

package  
{
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Travis Allen
	 */	
	public class Hero extends MovieClip
	{
		private var x_speed = 0;
		private var y_speed = 0;
		
		private var st_x = 50; //starting location
		private var st_y = 50;
		
		var spudnick:Spudnick = new Spudnick;  // this is the problem spudnick = main.as
		private var gravity = spudnick.gravity;
		private var walkspeed = spudnick.walkspeed;
		
		public function Hero() 
		{
			x = st_x;
			y = st_y;
		}
		public function get_hero():void
		{
			y_speed += gravity;
			if (leftKey)
			{
				x_speed = -walkspeed;
			}
			if (rightKey)
			{
				x_speed = walkspeed;
			}
			if (spaceKey)
			{
				y_speed = -jumpspeed;
			}
			x_speed = 0;
		}
	}

}



Theres my hero.as so far.
Was This Post Helpful? 0
  • +
  • -

#11 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: Questions about as 3

Posted 30 November 2010 - 04:57 AM

What is Spudnick?
Was This Post Helpful? 0
  • +
  • -

#12 Aivenn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 04-November 10

Re: Questions about as 3

Posted 30 November 2010 - 01:32 PM

var spudnick:Spudnick = new Spudnick; // this is the problem spudnick = main.as

its my document AS.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1