AS3 - Hit Test Object In different classes - actionscript-3

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;
}
}
}
}

Related

AS3 Classes Public Atributte

I am getting an error whenever I try running the game. I get errors for every class saying that the public attribute can only be used inside a package and on this line with "private function moveMe" "the private attribute may only be used on class functions". I verified if I have the as file linked properly. I am not sure what the issue is.
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.Event;
public class banana_fall extends MovieClip {
public function banana_fall (){
var velX:Number=0;
var velY:Number=0;
var falling:Boolean=false;
var gravity:Number=2;
public function banana() {
var timing:Timer = new Timer(20,0);
timing.addEventListener(TimerEvent.TIMER,moveMe);
timing.start();
}
private function moveMe(event:TimerEvent){
this.x=this.x+velX;
this.y=this.y+velY;
if (falling) {
velY=velY+gravity;
}
}
public function setSpot(atX,atY){
this.x=atX;
this.y=atY;
}
//
public function setSpeed(dx,dy){
velX=dx;
velY=dy;
}
}
}
}
The problem is that your public functions were inside your constructor function, which doesnt work in as3.
try this code:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.Event;
public class banana_fall extends MovieClip {
var velX: Number = 0;
var velY: Number = 0;
var falling: Boolean = false;
var gravity: Number = 2;
public function banana_fall() {
var timing: Timer = new Timer(20, 0);
timing.addEventListener(TimerEvent.TIMER, moveMe);
timing.start();
}
private function moveMe(event: TimerEvent) {
this.x = this.x + velX;
this.y = this.y + velY;
if (falling) {
velY = velY + gravity;
}
}
public function setSpot(atX, atY) {
this.x = atX;
this.y = atY;
}
//
public function setSpeed(dx, dy) {
velX = dx;
velY = dy;
}
}
}

Movement not working AS3

I'm currently working on a game, and am fairly new to AS3.
I'm stuck on the character movement: I was following a guide and ended up with the following as my code. When I test the game it just plays the character animation and I can't control it.
package {
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.Proxy;
import flash.utils.flash_proxy;
dynamic public class KeyObject extends Proxy {
private static var stage:Stage;
private static var keysDown:Object;
public function KeyObject(stage:Stage) {
construct(stage);
}
public function construct(stage:Stage):void {
KeyObject.stage = stage;
keysDown = new Object();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
flash_proxy override function getProperty(name:*):* {
return (name in Keyboard) ? Keyboard[name] : -1;
}
public function isDown(keyCode:uint):Boolean {
return Boolean(keyCode in keysDown);
}
public function deconstruct():void {
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
keysDown = new Object();
KeyObject.stage = null;
}
private function keyPressed(evt:KeyboardEvent):void {
keysDown[evt.keyCode] = true;
}
private function keyReleased(evt:KeyboardEvent):void {
delete keysDown[evt.keyCode];
}
}
}
package {
import flash.display.Sprite
import flash.events.Event;
import KeyObject;
public class Main extends Sprite{
private var key:KeyObject;
public function Main() {
addEventListener(Event.ADDED_TO_STAGE,setupKeyObject);
}
function setupKeyObject(e:Event){
key = new KeyObject(stage);
stage.addEventListener(Event.ENTER_FRAME,movePlayer);
}
function movePlayer(e:Event){
if(key.isDown(key.LEFT)){
roy.x -= 5;
}
if(key.isDown(key.RIGHT)){
roy.x +=5;
}
if(roy.x<0){
roy.x = 0;
}
if(roy.x > (stage.stageWidth - player.width)){
roy.x = stage.stageWidth - player.width;
}
}
}
}

AS3 duplicated MovieClip

In my app I'm importing movieclips from library to the stage like this :
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.ui.Keyboard;
import flash.ui.Mouse;
import flash.display.MovieClip;
public class MainTimeline extends MovieClip {
//Variabili
public var VFullscreen: int = 1;
//Import var
public var VTerminal: Terminal = new Terminal();
public var nTerminal:String;
public function MainTimeline(): void {
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
//Import
//Terminal
VTerminal.x = 288;
VTerminal.y = 384;
stage.addChild(VTerminal);
//Event Listeners
//addEventListener(MouseEvent.CLICK, fl_BringToFront);
VTerminal.addEventListener(MouseEvent.MOUSE_DOWN, fl_WindowDrag);
VTerminal.addEventListener(MouseEvent.MOUSE_UP, fl_WindowDrop);
}
//public functions
//Gestione Fullscreen
public function fl_Fullscreen(event: MouseEvent): void {
switch (VFullscreen) {
case 0:
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
VFullscreen = 1;
break;
case 1:
stage.displayState = StageDisplayState.NORMAL;
VFullscreen = 0;
break;
}
}
public function fl_FSCheck(event: Event): void {
if (stage.displayState == StageDisplayState.NORMAL) {
VFullscreen = 0;
}
if (stage.displayState == StageDisplayState.FULL_SCREEN_INTERACTIVE) {
VFullscreen = 1;
}
}
//Primo Piano Finestre
public function fl_BringToFront(event: MouseEvent): void {
this.addChild(event.currentTarget as DisplayObject);
}
public function fl_WindowDrag(event: MouseEvent): void {
event.currentTarget.startDrag();
nTerminal = event.currentTarget.name.toString();
trace(nTerminal);
}
public function fl_WindowDrop(event: MouseEvent): void {
event.currentTarget.stopDrag();
}
//Chiusura
public function fl_Close(event: MouseEvent): void {
stage.nativeWindow.close();
}
//Apertura/Chiusura Terminal
public function fl_Terminal(event: MouseEvent): void {
if (contains(VTerminal)) {
removeChild(VTerminal);
} else {
VTerminal.x = 288;
VTerminal.y = 320;
addChild(VTerminal);
}
}
}
}
But I've a strange bug that I've never seen. It had the same MC twice and, in the runtime, when i drag and drop it I can see 2 MC one named instance8 and one instance45. I don't know how to solve this.
Thanks in advance.
TERMINAL CLASS
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.ui.Keyboard;
import flash.text.TextField;
public class Terminal extends MovieClip {
public var version: String = "Verison 0.0.1 a";
public function Terminal(event: Event) {
//Varie
nome.text = "terminal";
vOut.text = version;
//Animazioni
loader0.gotoAndPlay(1);
loader1.gotoAndPlay(25);
}
}
}
As far as I can see, the statement
//Primo Piano Finestre
public function fl_BringToFront(event: MouseEvent): void {
this.addChild(event.currentTarget as DisplayObject);
}
when the mouseDown and mouseUp event finished, the mouseEvent.click event will trigger, so you will get another instance

Make a child call a function from its parent AS3

So I've been reading a lot about inheritance and circular dependency and I see no way around this. Still a little new to multiple classes. My document class creates Field. My Field class creates player and pushes bitmapData from the Tile MovieClip. I want player to be able to draw that bitmapData to the field. Engine.as(document class):
package {
import flash.display.MovieClip;
public class Engine extends MovieClip {
private var field:Field;
public static var _tilesData:Array = [];
public function Engine()
{
field = new Field();
field.x = 0;
field.y = 0;
addChild(field);
}
}
Field.as:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;
public class Field extends MovieClip{
private var player:Player;
private var sampleTile:Tile = new Tile();
public function Field()
{
player = new Player();
player.x = 0;
player.y = 0;
addChild(player);
GetSampleTiles();
}
public function GetSampleTiles()
{
for (var i:int = 0;i < 3; i++)
{
sampleTile.gotoAndStop(i);
var graphicData:BitmapData = new BitmapData(32,32);
graphicData.draw(sampleTile);
Engine._tilesData.push(graphicData);
}
}
public function DrawATile(tileToDraw:BitmapData)
{
var newTile:Bitmap = new Bitmap(tileToDraw);
newTile.x = player.x;
newTile.y = player.y;
addChild(newTile);
}
}
}
Player.as:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;
public class Player extends MovieClip
{
public var _inp:Input = new Input();
public function Player()
{
addChild(_inp);
addEventListener(Event.ENTER_FRAME, HandleKeys);
}
public function HandleKeys(e:Event)
{
if(_inp.keyUp)
{
y -= 32;
}
if(_inp.keyDown)
{
y += 32;
}
if(_inp.keyLeft)
{
x -= 32;
}
if(_inp.keyRight)
{
x += 32;
}
if(_inp.keySpace)
{
parent.DrawATile(Engine._tilesData[0]);
}
}
}
}
Is there a better way of doing this than I am seeing? When I put DrawATile function in Player.as it works... but the tiles move around with the player.
I placed the DrawATile function in Player.as and in it I add the newTile to parent like this - parent.addChild(newTile). that way it adds it to whatever its parent is and not to itself.

addChild/init issue

hey im doing the "Flash Bubbles: Paricle Systems with TimelineMax" from youtube. i got this code:
package
{
import com.greensock.*;
import com.greensock.easing.*;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class main extends Sprite
{
private var BG:bg= new bg;
private var start_btn:start_button= new start_button;
private var tl:TimelineMax= new TimelineMax();
private var bubbleMax:Number = 50;
public function main()
{
stage.addChild(BG);
stage.addChild(start_btn);
BG.y= (stage.stageWidth)/2;
BG.x = (stage.stageWidth)/2;
start_btn.x = (stage.stageWidth)/2;
start_btn.y = (stage.stageHeight)/2;
start_btn.addEventListener(MouseEvent.CLICK,StartGame);
}
private function StartGame(e:Event)
{
stage.removeChild(BG);
stage.removeChild(start_btn);
createBubble();
}
private function createBubble()
{
var Bubble:bubble= new bubble();
Bubble.y=200;
Bubble.x= randomRange(50,1100);
Bubble.alpha= .5;
addChild(Bubble);
}
private function randomRange(min:Number,Max:Number):Number
{
return min + (Math.random() * (Max - min));
}
private function init()
{
for (var count:Number = 0; count<bubbleMax; count++)
{
createBubble();
}
}
init();
}
}
bg, start_button, and bubble are all movie clips made in flash (and been given a as3 class)
I'm aspect to 50 bubbles, but can only see one.. thanks for help!
Change the StartGame function to this:
private function StartGame(e:Event)
{
stage.removeChild(BG);
stage.removeChild(start_btn);
// Your init function is where you're creating all the bubbles. That's what the "for" loop is doing.
init();
}
Also, don't call init(); towards the bottom like you are doing. Your code should look more like this:
package
{
import com.greensock.*;
import com.greensock.easing.*;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class main extends Sprite
{
private var BG:bg= new bg();
private var start_btn:start_button= new start_button();
private var tl:TimelineMax= new TimelineMax();
private var bubbleMax:Number = 50;
public function main()
{
stage.addChild(BG);
stage.addChild(start_btn);
BG.y= (stage.stageWidth)/2;
BG.x = (stage.stageWidth)/2;
start_btn.x = (stage.stageWidth)/2;
start_btn.y = (stage.stageHeight)/2;
start_btn.addEventListener(MouseEvent.CLICK,StartGame);
}
private function StartGame(e:Event)
{
stage.removeChild(BG);
stage.removeChild(start_btn);
init();
}
private function createBubble()
{
var Bubble:bubble= new bubble();
Bubble.y=200;
Bubble.x= randomRange(50,1100);
Bubble.alpha= .5;
addChild(Bubble);
}
private function randomRange(min:Number,Max:Number):Number
{
return min + (Math.random() * (Max - min));
}
private function init()
{
for (var count:Number = 0; count<bubbleMax; count++)
{
createBubble();
}
}
}
}