1 Replies - 1703 Views - Last Post: 27 December 2011 - 10:18 PM

#1 helpme18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 21-October 09

Help with Actionscript throwing object.

Posted 16 November 2011 - 10:26 AM

Im having trouble building a bullet class for my program. The purpose of this program is to control your character(faulkner) with the arrow keys while trying to dodge dodgeballs thrown by enemies currently undefined in the code. In the FLA file, the enemies are given simple motion tweens and i want those enemies to throw the dodgeballs. Could someone please help me start to build/understand the code needed for the motion tweened enemies in the FLA file to throw the dodgeballs.

package 
{

	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.Event;

	public class Main_Character extends MovieClip
	{
		var vx:int;
		var vy:int;
		private var _dodgeball:Array;
		var hits:uint;
		var collision:Boolean;

		public function Main_Character()
		{
			init();
			_dodgeballs= new Array();
			_dodgeballs= [];
			stage.addEventListener("bulletCreated", onBulletCreated)
		}
		
		private function onBulletCreated(event:Event)
		{
			//Add new bullet to the _bullets array
			_dodgeball.push(MovieClip(event.target));
			trace(event.target.name);
		}
		function init():void
		{
			//initialize variables
			vx = 0;
			vy = 0;
			hits= 0;
			collision=false;
			faulkner.stop();

			//Add event listeners
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onkeydown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onkeyup);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		function onkeydown(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.LEFT)
			{
				faulkner.gotoAndStop(4);
				vx = -7;
			}
			else if (event.keyCode == Keyboard.RIGHT)
			{
				faulkner.gotoAndStop(3);
				vx = 7;
			}
			else if (event.keyCode == Keyboard.UP)
			{
				faulkner.gotoAndStop(1);
				vy = -7;
			}
			else if (event.keyCode == Keyboard.DOWN)
			{
				faulkner.gotoAndStop(2);
				vy = 7;
			}
		}
		function onkeyup(event:KeyboardEvent):void
		{
			if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
			{
				vx = 0;
			}
			else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
			{
				vy = 0;
			}
			
		}
		function onEnterFrame(event:Event):void
		{
			//Move the player
			var playerWidth:uint= faulkner.width/2;
			var playerHeight:uint= faulkner.height/2;
			
			faulkner.x += vx;
			faulkner.y += vy;
			
			
			if(faulkner.x+playerWidth>stage.stageWidth){
				faulkner.x=stage.stageWidth-playerWidth;
			}
			else if(faulkner.x-playerWidth<0)
			{
				faulkner.x=0+playerWidth;
			}
			
			else if(faulkner.y-playerHeight<0)
			{
				faulkner.y=0+playerHeight;
			}
			else if(faulkner.y+playerHeight>stage.stageHeight)
			{
				faulkner.y=stage.stageHeight-playerHeight;
			}
			
			//Collision detection and counts the amount of collisions.
			if (faulkner.hitTestObject(ball))
			{
				
				
				if(! collision){
				hits++;
				messageDisplay.text=String(hits);
				collision=true;
				}
			}
			else
			{
				messageDisplay.text="";
				collision=false;
			}
			
			if(hits>=3)
			{
				messageDisplay2.text="YOU LOSE";
			}
		}
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Help with Actionscript throwing object.

#2 RuneChart  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 13-July 11

Re: Help with Actionscript throwing object.

Posted 27 December 2011 - 10:18 PM

How do you want the enemies to throw the balls? Randomly, or will they aim? Will they throw them according to a timer, or when the player gets close to them, or what?

At any rate, I'd do a bullet class simply like this:
public class Bullet extends flash.display.MovieClip {
	public var xSpeed : Float;
	public var ySpeed : Float;
}


When you create a new Bullet, you'll set its x and y values equal to the enemy that threw it. You'll then adjust its speed depending on your answers to my questions above.

I don't think you really need that bulletCreated event launcher. You can call the function to create add a new bullet directly. You'll need to expand it a bit though:
public function onBulletCreated () {
	var bullet : Bullet = new Bullet();
	//set bullet.x,bullet.y,bullet.xSpeed,bullet.ySpeed
	stage.addChild(bullet);
	_dodgeball.push(bullet);
}
Let me know if I need to go into more detail.

EDIT: Didn't realize how old this post was. I assume the issue is solved?

This post has been edited by RuneChart: 27 December 2011 - 10:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1