How can i hitTesObject a movieclip inside a movieclip AS3? - actionscript-3

I want my zombie to collect the brains(g1b1,g1b2,g1b3,g1b4). These brains are inside my movieclip 'platform1' because i need it to have a scrolling background effect. Well my zombie character can walk however he can't collect the brains.
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.display.Sprite;
public class z extends MovieClip {
private var score:int=0;
private var zombieSpeed:int=7; //speed for zombie
private var gravity:int=1;
private var jumpSpeed:int=0;//current speed for the jump
private var jumpSpeedLimit:int=15; //how quick the jump
private var jump, left, right, down, mainJump:Boolean=false; //zombie is not jumping
public function z() {
}
public function Game1() {
stop();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
zombie.addEventListener(Event.ENTER_FRAME, moveCharacter1);
gamePlay1();
}
private function keyPressed(e:KeyboardEvent) {
if (e.keyCode==Keyboard.LEFT) {
left=true;
zombie.gotoAndPlay("left");
}
if (e.keyCode==Keyboard.RIGHT) {
right=true;
zombie.gotoAndPlay("right");
}
if (e.keyCode==Keyboard.SPACE || Keyboard.UP) {
jump=true;
}
if (e.keyCode==Keyboard.DOWN) {
down=true;
}
}
private function keyReleased(e:KeyboardEvent) {
if (e.keyCode==Keyboard.LEFT || Keyboard.D) {
left=false;
zombieSpeed-=5;
zombie.gotoAndStop("idleLeft");
}
if (e.keyCode==Keyboard.RIGHT || Keyboard.A) {
right=false;
zombieSpeed+=5;
zombie.gotoAndStop("idleRight");
}
if (e.keyCode==Keyboard.SPACE || Keyboard.UP || Keyboard.W) {
jump=false;
}
if (e.keyCode==Keyboard.DOWN || Keyboard.S) {
down=false;
}
}
private function moveCharacter1(e:Event) {
if (left) {
if (platform1.x<1000 && zombie.x==870) {
platform1.x+=5;
}
else {
if (zombie.x>120) {
zombie.x-=zombieSpeed;
trace(platform1.x);
}
}
}
if (right) {
if (platform1.x>0 && zombie.x==870) {
platform1.x-=5;
}
else {
if (zombie.x<880) {
zombie.x+=zombieSpeed;
trace(platform1.x)
}
}
}
if (jump) {
goJump();
}
function goJump() {
if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
jumpSpeed *= 1 + jumpSpeedLimit/50;
}
zombie.y += jumpSpeed;
for (var i=0;i<5;i++) {
if(platform1.hitTestPoint(zombie.x,zombie.y,true)) {
zombie.y--;
jumpSpeed=0;
mainJump=false;
}
}
}
**private function gamePlay1() {
if(zombie.hitTestObject(platform1.g1b1)){
platform1.g1b1.visible = false;
score+=10;
}
if(zombie.hitTestObject(platform1.g1b2)){
platform1.g1b2.visible = false;
score+=10;
}
if(zombie.hitTestObject(platform1.g1b3)){
platform1.g1b3.visible = false;
score+=10;
}
if(zombie.hitTestObject(platform1.g1b4)){
platform1.g1b4.visible = false;
score+=10;
}
}**
}

Related

Movement Keys not Working

Nothing is happening when I press the arrow keys, but neither are there any errors: what's wrong with this? If I remove the key press testing it accelerates accordingly...
At this stage I am just trying to move a block around a screen in an inertial manner using the arrow keys. However, this is my first foray into AS3 so I may be going about it in completely the wrong manner.
Any help would be greatly appreciated.
Unit.AS:
package {
import flash.display.MovieClip;
import flash.events.*
import flash.ui.Keyboard
public class Unit extends MovieClip {
var velocityX:Number = 1;
var velocityY:Number = 1;
var accellerationX:Number = 1;
var accellerationY:Number = 1;
public function Unit(){
addEventListener("enterFrame", move);
}
private function move(e:Event){
accellerate()
this.x += velocityX;
this.y += velocityY;
}
private function accellerate(){
if (Key.isDown(Keyboard.UP)){
velocityY += accellerationY;
trace("Accellerating");
}
if (Key.isDown(Keyboard.DOWN)){
velocityY -= accellerationY;
trace("Accellerating");
}
if (Key.isDown(Keyboard.RIGHT)){
velocityX += accellerationX;
trace("Accellerating");
}
if (Key.isDown(Keyboard.LEFT)){
velocityX -= accellerationX;
trace("Accellerating");
}
}
}
}
Key.AS:
package
{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Key {
private static var initialized:Boolean = false;
private static var keysDown:Object = new Object();
public static function initialize(stage:Stage) {
if (!initialized) {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.DEACTIVATE, clearKeys);
initialized = true;
}
}
public static function isDown(keyCode:uint):Boolean
{
return Boolean(keyCode in keysDown);
}
private static function keyPressed(event:KeyboardEvent):void {
keysDown[event.keyCode] = true;
}
private static function keyReleased(event:KeyboardEvent):void {
if (event.keyCode in keysDown) {
delete keysDown[event.keyCode];
}
}
private static function clearKeys(event:Event):void {
keysDown = new Object();
}
}
}
On your unit constructor function call the initialize(stage) static function.
Key.initialize(stage);

Error when debugging Flash Game

I keep on getting this error
TypeError: Error #1010: A term is undefined and has no properties.
at GameController/update()[C:\Users\Administrator\Desktop\Better PW\GameController.as:32]
I have the terms defined, yet it keeps giving me an error. Please Help Debug! TY.
package
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
public class GameController extends MovieClip
{
private var score1, score2, year, delay, p1, p2:Number;
private var phase, game_winner:String;
private var playDone:Boolean;
public function GameController()
{}
private function update(evt:Event){
if(p1 == 1 && p2 == 1){
score1= score1+1;
score2= score2+1;
}else if(p1 == 1 && p2 == 2){
score1= score1+3
}else if (p2 == 1 && p1 == 2){
score2= score2+3
}else if (p1 == 2 && p2 == 2){
score1= score1+2
score2= score2+2
}
txtPlayerUno.text = score1.toString();
txtPlayerDous.text = score2.toString();
p1 = 0
p2 = 0
}
private function playerRound(){
}
public function startmenu()
{
stop();
btnStartGame.addEventListener(MouseEvent.CLICK, gotoStartGame);
btnHowToPlay.addEventListener(MouseEvent.CLICK, gotohowtoplay);
}
private function gotoStartGame(evt:MouseEvent)
{
btnStartGame.removeEventListener(MouseEvent.CLICK, gotoStartGame);
btnHowToPlay.removeEventListener(MouseEvent.CLICK, gotohowtoplay);
gotoAndStop("game");
}
public function starthowtoplay()
{
btnBack.addEventListener(MouseEvent.CLICK, gotoMenu);
}
private function gotohowtoplay(evt:MouseEvent)
{
btnStartGame.removeEventListener(MouseEvent.CLICK, gotoStartGame);
btnHowToPlay.removeEventListener(MouseEvent.CLICK, gotohowtoplay);
gotoAndStop("howtoplay");
}
public function startgame() :void
{
addEventListener(Event.ADDED_TO_STAGE, gameAddedToStage );
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
}
private function gotoMenu(evt:MouseEvent)
{
btnBack.removeEventListener(MouseEvent.CLICK, gotoMenu);
gotoAndStop("menu");
}
private function keyDownHandler(evt:KeyboardEvent):void
{
//1- War 2-Peace
if (evt.keyCode == 37)
{
p1= 2
}
else if (evt.keyCode == 39)
{
p1= 1
}
if (evt.keyCode == 65)
{
p2=2
}
else if (evt.keyCode == 68)
{}
}
private function gameAddedToStage(evt: Event):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
}
}
}

AS3 - Hit Test Object In different classes

I am making pong
I need the ball to sense detection of the walls and two paddles
How can I achieve this without making a central class and just keep the 3 different classes for Paddle 1 paddle 2 and Ball?
BALL CLASS
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Grey extends MovieClip {
/*var blues:Array = new Array
var blue:Blue = new Blue();*/
private var changes:Boolean
private var directions
private var speed:int = 20;
public function Grey() {
// constructor code
stage.addEventListener(Event.ENTER_FRAME, loop)
/*stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed)*/
}
function loop(e:Event) {
movements();
hitTesting();
}
function movements() {
if (changes==true) {
directions = -1;
} else {
directions = 1;
}
x+=speed * directions;
}
function hitTesting() {
/*if (this.hitTestObject(blues)) {
changes = true;
}*/
}
}
}
Paddle 1 Class (PLAYER)
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Blue extends MovieClip {
private var speed:int = 10;
public function Blue() {
// constructor code
stage.addEventListener(Event.ENTER_FRAME, loop);
/*stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed)*/
}
function loop(e:Event) {
if (parent.mouseY > 0 && parent.mouseY < 360) {
y = parent.mouseY;
}
}
/*function pressed(e:KeyboardEvent) {
switch(e.keyCode) {
case 38:
y-=speed;
break;
case 40:
y+=speed;
break;
}
}*/
}
}
Paddle 2 Class (CPU)
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Red extends MovieClip {
var greyy:Grey = new Grey();
private var speed:int = 10;
public function Red() {
// constructor code
stage.addEventListener(Event.ENTER_FRAME, loop);
/*stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed)*/
}
function loop(e:Event) {
if (greyy.y > y) {
y-=3;
} else if(greyy.y < y){
y+=3;
}
}
}
}

Keep receiving error: access of undefined property menu in AS3

I am following a tutorial (http://flashdeveloptutorial.blogspot.com/2011/08/chapter-2.html) for AS3 using flash develop to create a pong clone and am having trouble with displaying a main menu. I previously had a functioning prototype with a moving paddle so I believe the error(s) is in my revised(or new) Main, MainMenu, PongGame or CustomEvents file(s). When I run the code as instructed through the tutorial I receive the error ; access of undefined property menu in my Main.as file:
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* #author
*/
public class Main extends Sprite
{
private var game:PongGame;
****private var menu:MainMenu;****
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
private function buildMenu():void {
menu = new MainMenu();
addChild(menu);
menu.addEventListener(CustomEvents.LAUNCH_GAME, startGame, false, 0, true);
}
private function startGame(e:CustomEvents):void {
removeChild(menu);
menu.removeEventListener(CustomEvents.LAUNCH_GAME, startGame);
menu = null;
game = new PongGame();
addChild(game);
}
}
}
So I added the "private var menu:MainMenu;" section but I do not know what class/ .as file to reference? or how to define var menu:
Sorry for the large amount of attached code. I did not change paddle.as which is the longest file.
assets.as
package
{
public class Assets
{
[Embed (source = '../lib/paddle.png')] public static const Pad:Class;
public function Assets(): void {
}
}
}
Paddle.as
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Paddle extends Sprite {
private var pic:Bitmap = new Assets.Pad();
public function Paddle():void {
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function die(e:Event):void {
removeChild(pic);
pic = null;
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
removeEventListener(Event.ENTER_FRAME, enterFrame);
removeEventListener(Event.REMOVED_FROM_STAGE, die);
}
private function go(e:Event) : void {
removeEventListener(Event.ADDED_TO_STAGE, go);
addChild(pic);
y = stage.stageHeight - pic.height;
x = stage.stageWidth * .5 - pic.width * .5;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false, 0, true);
addEventListener(Event.ENTER_FRAME, enterFrame, false, 0, true);
addEventListener(Event.REMOVED_FROM_STAGE, die);
}
private var movingLeft:Boolean;
private var movingRight:Boolean;
private function enterFrame(e:Event):void {
if (movingLeft) {
if(x-speed >= 0) {
x -= speed;
}
else {
x = 0;
}
}
else if (movingRight) {
if (x + speed + pic.width <= 600) {
x += speed;
}
else {
x = 600 - pic.width;
}
}
}
private var speed:int = 10;
private function keyDownHandler(e:KeyboardEvent):void {
if (e.keyCode == 38 || e.keyCode == 87) {
if (!movingLeft) {
movingLeft = true;
}
}
else if (e.keyCode == 40 || e.keyCode == 83) {
if (!movingRight) {
movingRight = true;
}
}
}
private function keyUpHandler(e:KeyboardEvent):void {
if (e.keyCode == 38 || e.keyCode == 87) {
if (movingLeft) {
movingLeft = false;
}
}
if (e.keyCode == 40 || e.keyCode == 83) {
if (movingRight) {
movingRight = false;
}
}
}
}
}
mainmenu.as
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
public class MainMenu extends Sprite
{
private var pongButton:Sprite;
public function MainMenu():void{
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function go(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, go);
pongButton = item("Play", 20, 30, launchGame, 0xFF0000);
addChild(pongButton);
}
private function launchGame(e:MouseEvent):void {
dispatchEvent(new CustomEvents(CustomEvents.LAUNCH_GAME));
}
private function item(buttonText:String, X:int, Y:int, Funct:Function, txtColor:uint = 0xFFFFFF):Sprite {
var item:Sprite = new Sprite();
item.graphics.beginFill(0);
item.graphics.lineStyle(1, txtColor, .5);
item.graphics.drawRect(0, 0, 250, 30);
var myText:TextField = new TextField();
myText.selectable = false;
myText.width = 250;
myText.height = 30;
item.addChild(myText);
myText.autoSize = "center";
myText.text = buttonText;
myText.textColor = txtColor;
item.addEventListener(MouseEvent.CLICK, Funct);
item.x = X;
item.y = Y;
return item;
}
}
}
PongGame.as
package
{
import flash.display.Sprite;
import flash.events.Event;
public class PongGame extends Sprite
{
private var paddle:Paddle;
public function PongGame():void {
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function go(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, go);
paddle = new Paddle();
addChild(paddle);
}
}
}
CustomEvents.as
package
{
import flash.events.Event;
public class CustomEvents extends Event
{
public static const LAUNCH_GAME:String = "launch_game";
public function CustomEvents(e:String):void {
super(e);
}
}
}
I'm not sure where to go from here to fix this error. Any help would be appreciated.
Although you've correctly added the menu property, the menu is never displayed because buildMenu() function is never called.
From your main class init(), call buildMenu() to instantiate and add menu to the stage:
Main.as
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
buildMenu();
}

File not found AS3

I am writing a program in AS3. It's exact contents don't seem to matter here. I basically have 4 classes, split in 4 files. Thing is, it can't find one of these files, and I can't understand why. I have checked it several times and even had my teacher check it, and we still can't find what is wrong with it. Did anyone else have a similar problem, or have any idea on how to solve this problem?
EDIT:
Line 37 1046: Type was not found or was not a compile-time constant: CustomTextField.
Line 37 1180: Call to a possibly undefined method CustomTextField. (Twice)
package
{
// Import stuff
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.*;
public class CustomButton extends MovieClip
{
// Private variables:
private var animationsListAR_:Array = [];// Array for the animations.
private var textArea_:CustomTextField = new CustomTextField();
private var curState_:int = 1;// The current state (1 = Normal, 2 = Pressed, 3 = Over).
private var active_:Boolean;
private var type_:String;// Multi choice, single choice, normal.
private var group_:int;
private var animated_:Boolean = false;
// Protected variables:
// Public variables:
// Constructor:
// This constructor sets up the event listeners and the button's properties.
public function CustomButton( animationAR:Array, buttonlabel:String = "Hello", animated:Boolean = false, active:Boolean = true, type:String = "free", group:int = 0 )
{
this.gotoAndStop( curState_ );
// Prevents the start of the animations.
// Deals with the text inside the button.
this.buttonMode = true;
active_ = active;
this.addChild( textArea_ );
if (animated == true)
{
animated_ = true;
/*
animationsListAR_[0] = 1;
for (var i:int = 1; i < animationAR.length; i++)
{
animationsListAR_[i] = animationAR[i - 1];
}
*/
}
// If active_ is true the game will add EventListeners to this object.
if (active_ == true)
{
this.addEventListener(MouseEvent.MOUSE_DOWN,mouseOverHandler);
this.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler);
this.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler);
}
// Needed to monitor the active_ var.
this.addEventListener(Event.CHANGE, activeChangeHandler);
}
// This function swaps the active_ variable value and calls activeChangeHandler.
public function ActiveChange()
{
active_ = ! active_;
this.dispatchEvent(new Event(Event.CHANGE));
}
public function mouseOverHandler( evt:MouseEvent )
{
if(evt.type == MouseEvent.MOUSE_DOWN)
{
curState_ = 2;
if (animated_ == true)
{
loopSegment( ( animationsListAR_[1] + 1 ), animationsListAR_[2] );
}
else
{
this.gotoAndStop( curState_ );
}
this.addEventListener( MouseEvent.MOUSE_UP, function( evt:MouseEvent ):void
{
evt.target.removeEventListener( MouseEvent.MOUSE_DOWN, arguments.callee );
curState_ = 3;
evt.target.gotoAndStop( curState_ );
});
}
else if( evt.buttonDown != true )
{
curState_ = 3;
if (animated_ == true)
{
loopSegment( ( animationsListAR_[2] + 1 ), animationsListAR_[3] );
}
else
{
this.gotoAndStop( curState_ );
}
}
dispatchEvent(new CustomButtonEvent(CustomButtonEvent.OVER));
}
public function mouseOutHandler(evt:MouseEvent)
{
curState_ = 1;
if (animated_ == true)
{
loopSegment( ( animationsListAR_[0] ), animationsListAR_[1] );
}
else
{
this.gotoAndStop( curState_ );
}
dispatchEvent(new CustomButtonEvent(CustomButtonEvent.OUT));
}
public function activeChangeHandler(evt:Event)
{
if (active_ == true)
{
this.addEventListener(MouseEvent.CLICK,mouseOverHandler);
this.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler);
this.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler);
}
else
{
this.removeEventListener(MouseEvent.CLICK,mouseOverHandler);
this.removeEventListener(MouseEvent.MOUSE_OVER,mouseOverHandler);
this.removeEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler);
}
// modifyMaskButton();
}
/*
public function modifyMaskButton():void
{
if (active_ = true)
{
}
else
{
}
}
*/
public function playSegment( begin:int, end:int ):void
{
if (begin != end)
{
gotoAndPlay( begin );
addEventListener(Event.ENTER_FRAME,function( evt:Event ):void
{
if( currentFrame == end )
{
stop();
removeEventListener(Event.ENTER_FRAME, arguments.callee);
}
});
}
else
{
this.gotoAndStop( end );
}
}
// This loops continuosly an animation. Should, at least...
public function loopSegment( begin:int, end:int ):void
{
if (begin != end)
{
gotoAndPlay( begin );
addEventListener(Event.ENTER_FRAME,function( evt:Event ):void
{
if( currentFrame == end )
{
gotoAndPlay( begin );
}
});
}
else
{
this.gotoAndStop( end );
}
}
}
}
Second File:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
public class CustomTextField extends MovieClip
{
private var textArea:TextField;
private var boldText:TextFormat = new TextFormat();
function CustomTextField()
{
textArea = new TextField;
}
override function CustomTextfield( labelText:String, font:String = "Arial", color:uint = 0xFFFFFFFF, size:uint = 10 )
{
textArea = new TextField;
boldText.font = font;
boldText.color = color;
boldText.size = size;
this.text = labelText;
}
function changeFont( font:String ):void
{
boldText.font = font;
updateText();
}
function changeColor( color:uint ):void
{
boldText.color = color;
updateText();
}
function changeSize( size:uint ):void
{
boldText.size = size;
updateText();
}
function embedText( choice:Boolean ):void
{
this.embedFonts = choice;
}
function updateText():void
{
this.setTextFormat( boldText );
}
}
}
Check this line in the second file:
override function CustomTextfield( labelText:String, font:String = "Arial"
"CustomTextfield" // should be "CustomTextField",within your script missing the capital letter.
I think your getting 2 errors as this function is triggered twice within script or some related change it and let us know.