How do I get Hit Detection to work in Flash? - actionscript-3

Ives tried many hit detections and none of them seem to work for me. I've tried hittest hittestobject hitarea. When my object (which is a or b movie-clip goes fully into c movie clip i want c to move 300 x direction. Does not need to be pin point detection just as long as its in the c movie-clip it works.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.media.Sound;
import flash.ui.Mouse;
public class bakingCake extends MovieClip {
public function bakingCake() {
// constructor code
var object:MovieClip = new MovieClip;
a.addEventListener(MouseEvent.MOUSE_DOWN,objectA);
b.addEventListener(MouseEvent.MOUSE_DOWN,objectB);
if (object.hitArea(c) == true)
{
c.x = 300;
}
function objectA():void
{
object = a;
object.addEventListener(MouseEvent.MOUSE_OVER,objectFun);
}
function objectB():void
{
object = b;
object.addEventListener(MouseEvent.MOUSE_OVER,objectFun);
}
function objectFun(event:MouseEvent):void
{
object.addEventListener(MouseEvent.MOUSE_DOWN,drag);
object.addEventListener(MouseEvent.MOUSE_UP,sDrag);
}
function drag(event:MouseEvent):void
{
object.startDrag();
}
function sDrag(event:MouseEvent):void
{
object.stopDrag();
}
}
}
}

Did you tried getBounds() ?
I suggest condition:
if (c.getBounds(c.parent).containsRect(a.getBounds(c.parent))
|| c.getBounds(c.parent).containsRect(b.getBounds(c.parent))) {
c.x = 300;
}
IMO the best way is check it triggered by ENTER_FRAME event, atached for any of object.

Related

Removing the own MovieClip through its class block

I'm trying to make a ship game and I'm having an problem to get opponents fading as well.
Well, these opponents (like ships) has an class. In this class I do a interval to make its children fly to left (changing X per velocity number choosen on fourth argument in the function for adding enemies (addOpponent(opponentX, opponentY, opponentType, opponentVelocity)) and, when any of them has coordinate X smaller than -25, must be removed, through class block of itself.
package {
import flash.display.*
import flash.display.MovieClip;
import flash.utils.setTimeout;
import flash.utils.setInterval;
import flash.utils.clearInterval;
public class opponentNave extends MovieClip {
public function opponentNave(opponentVelocitySet) {
var loopMoveClassicOpponentsNave:uint = setInterval(movingClassicOpponentNave, 58);
function movingClassicOpponentNave() {
if (x < -25) {
clearInterval(loopMoveClassicOpponentsNave);
this.parent.removeChild(this);
} else {
x -= opponentVelocitySet;
}
}
}
}
}
I'm using this.parent.removeChild(this). I'm getting a error when the opponent X is smaller than -25, and it's on that time I want to remove the opponent child.
Here is how I would refactor this: (see code comments)
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class opponentNave extends MovieClip
{
//create a class scoped variable for the velocity
private var velocitySet:Number;
public function opponentNave(opponentVelocitySet)
{
//set the velocity var
velocitySet = opponentVelocitySet;
//wait for this object (opponentNave) to be added to the display before doing anything display oriented
this.addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
}
private function addedToStage(e:Event):void {
//run a function every frame tick of the application's fps
//this is best for things that are display oriented instead of time based ways like Timer or Intervals
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(e:Event):void
{
if (x < -25){
if (this.parent) this.parent.removeChild(this);
this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
else
{
x -= velocitySet;
}
}
}
}

How do I fix the error 1120: Access of undefined property?

While sitting, watching and reading about framework, I tried it and cant get my program going.
So when I programmed I had 3 frames. One for pre loader, one for Game (no menu, just straight to game), and one last one for me to keep notes and patch note etc in.
I coded in the frame. I didnt have any extra .as files or nothing, and it all works.
Then I tried converting to having a GameControler.as and a C.as (for constant values etc), and that didn't work.
So I started over, and ended up just trying it out and ended with this code:
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.utils.*;
import flash.ui.*;
import Game.*;
public class GameController extends MovieClip {
private var score: Number;
public function GameController() {
// constructor code
}
public function startGame() {
score = C.score;
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function scoreF(e: MouseEvent):void {
score = score + 1;
}
hitBtn.addEventListener(MouseEvent.CLICK, scoreF)
private function update(e: Event) {
score_n.text = String(score);
}
}
}
I end up with these two errors.
Line 30, Column 3 1120: Access of undefined property hitBtn.
Line 30, Column 45 1120: Access of undefined property scoreF.
What am I not understanding?
I just wanna click the button, witch is on stage, add up the score and update the on stage score.
Even though your question was answered, here is a pattern you might want to follow:
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.utils.*;
import flash.ui.*;
import Game.*;
public class GameController extends MovieClip {
private var hitBtn:MovieClip;
private var score: Number;
public function GameController() {
// constructor code
createChildren();
}
protected function createChildren():void {
// when it was not read from the display list
// or created in a subclass via inheritence
if (!hitBtn) {
hitBtn = getChildByName('hitBtn') as MovieClip;
if (hitBtn) {
hitBtn.addEventListener(MouseEvent.CLICK, scoreF);
} else {
trace('Child #hitBtn is not found or not a MovieClip.').
}
}
}
public function startGame() {
score = C.score;
if (stage) {
stage.addEventListener(Event.ENTER_FRAME, update);
} else {
trace("Attempt to start the game, although the controller is not added to stage.");
}
}
public function scoreF(e: MouseEvent):void {
score = score + 1;
}
private function update(e: Event) {
score_n.text = String(score);
}
}
}
When using Flash to add children, those are added to the MovieClip when it is created, so you can access them right away. Following the pattern will give you more safety when working on larger projects, which sometimes change ... this way you can get very fast an idea of what's wrong.

Recieving incorrect number of arguments error on a class that calls for zero

I've made a very simple 'game' where a circle is created and when clicked a new circle is added and clicking the new circle adds another while clicking any old circles 'ends' the game. I'm going to have a simple endgame screen with a restart button. I apologize if the code is messy, it is for practice in FlashDevelop.
The problem is I am trying to call my "endGame" class function in Main but I receive the error: "(58): col: 5 Error: Incorrect number of arguments. Expected 1." Line 58 is my call to endGame()
The weird part is that I receive the error even with endGame completely empty. I've posted both sections below and marked (**) the endGame call in main.
Main class
package Fun
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Shape;
import flash.events.MouseEvent;
public class Main extends Sprite
{
public var circles:Array;
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
makeacircle()
}
public function makeacircle():void
{
circles = new Array();
var addcirc:addcircle;
addcirc = new addcircle((Math.random() * 999999))
circles.push(addcirc)
addChild(addcirc)
addcirc.x = 300
addcirc.y = 200
addEventListener(eventclass.LOOP, movecirc);
addcirc.addEventListener(MouseEvent.CLICK, transition )
function transition(e:MouseEvent):void
{
addcirc.removeEventListener(MouseEvent.CLICK, transition);
dispatchEvent ( new eventclass(eventclass.LOOP))
addcirc.addEventListener(MouseEvent.CLICK, gameover)
}
function gameover(e:MouseEvent):void
{
for each (var circle:addcircle in circles)
{
removeChild(circle)
}
circles.length = 0
** endGame()
}
}
public function movecirc(Eventclass:eventclass):void
{
var addcirc:addcircle;
addcirc = new addcircle((Math.random() * 1000000))
circles.push(addcirc);
addcirc.addEventListener(MouseEvent.CLICK, looper)
addChild(addcirc);
for each (var circle:addcircle in circles)
{
circle.x = (Math.random() * 600)
circle.y = (Math.random() * 400)
}
function looper(e:MouseEvent):void
{
addcirc.removeEventListener(MouseEvent.CLICK, looper)
dispatchEvent ( new eventclass( eventclass.LOOP))
addcirc.addEventListener(MouseEvent.CLICK, gameover)
}
function gameover(e:MouseEvent):void
{
for each (var circle:addcircle in circles)
{
removeChild(circle)
}
circles.length = 0
}
}
}
}
endGame
package Fun
{
import flash.display.Sprite;
public class endGame extends Sprite
{
public function endGame():void
{
}
}
}
Without the call to endGame() the code runs perfect and everything I want occurs. I'm just not sure why the endGame function is claiming to require arguments.
We have several problems here.
First: you should ALWAYS use semicolon, not when you remember.
Second: the cause of problem is you are calling endGame() as if it's a function
You have a Class endGame so this is the correct use of it:
var eg : endGame = new endGame();
From what i see you should learn a bit more about OOP before using Classes

How to get a Numeric Stepper and sliders to work together in Flash CS4 AS3

Hi I am working on a piece of coursework for school and I want to have a page where I have sliders that reflect a numeric stepper and vice versa however whenever I find a tutorial online it keeps giving me errors, the most recent of which is 1120: Access of undefined property NumericStepperEvent. I am hesitant to continue with the rest of the steppers because of the fact that I am unsure how to get it to work but I need to have this section done by Friday. Any help will be much appreciated.
Thanks Matt
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import fl.controls.Slider;
import fl.events.SliderEvent;
import fl.controls.Label;
import fl.controls.NumericStepper;
public class Main extends MovieClip
{
//Variables
var startPage:StartPage;
var tutorialPage:TutorialPage;
var maleSizePage:MaleSizePage;
var femaleSizePage:FemaleSizePage;
public function Main()
{
//Pages
startPage = new StartPage ;
tutorialPage = new TutorialPage ;
maleSizePage = new MaleSizePage ;
femaleSizePage = new FemaleSizePage ;
addChild(startPage);
//Event Listeners
startPage.startButton.addEventListener(MouseEvent.CLICK,startButtonClick);
tutorialPage.continueButton.addEventListener(MouseEvent.CLICK,tutorialContinueButtonClick);
maleSizePage.heightSlider.addEventListener(SliderEvent.CHANGE,heightSlChange_M);
maleSizePage.neckSlider.addEventListener(SliderEvent.CHANGE,neckSlChange_M);
maleSizePage.chestSlider.addEventListener(SliderEvent.CHANGE,chestSlChange_M);
maleSizePage.waistSlider.addEventListener(SliderEvent.CHANGE,waistSlChange_M);
maleSizePage.armSlider.addEventListener(SliderEvent.CHANGE,armSlChange_M);
maleSizePage.legSlider.addEventListener(SliderEvent.CHANGE,legSlChange_M);
maleSizePage.heightValue.addEventListener(NumericStepperEvent.CHANGE,heightVChange_M);
}
function startButtonClick(event:MouseEvent):void
{
addChild(tutorialPage);
}
function tutorialContinueButtonClick(event:MouseEvent):void
{
if (startPage.maleSelection.selected == true)
{
addChild(maleSizePage);
removeChild(startPage);
removeChild(tutorialPage);
}
if (startPage.femaleSelection.selected == true)
{
addChild(femaleSizePage);
removeChild(startPage);
removeChild(tutorialPage);
}
}
function heightSlChange_M(Event:SliderEvent):void
{
maleSizePage.heightValue.value = maleSizePage.heightSlider.value;
}
function neckSlChange_M(Event:SliderEvent):void
{
maleSizePage.neckValue.value = maleSizePage.neckSlider.value;
}
function chestSlChange_M(Event:SliderEvent):void
{
maleSizePage.chestValue.value = maleSizePage.chestSlider.value;
}
function waistSlChange_M(Event:SliderEvent):void
{
maleSizePage.waistValue.value = maleSizePage.waistSlider.value;
}
function armSlChange_M(Event:SliderEvent):void
{
maleSizePage.armValue.value = maleSizePage.armSlider.value;
}
function legSlChange_M(Event:SliderEvent):void
{
maleSizePage.legValue.value = maleSizePage.legSlider.value;
}
function heightVChange_M(Event:NumericStepper)
{
maleSizePage.heightSlider.value = maleSizePage.heightValue.value;
}
}
}
You need to tell the compiler which classes to use. At the moment it has no idea what NumericStepperEvent is.
Add at the top with other imports add: import mx.events.NumericStepperEvent;
Also i'd recommend changing the argument variables written Event:SliderEvent to lowercase event:SliderEvent as this may cause another compiler error.

Access of undefined property Keyboard (AS3)

I'm new to Actionscript 3 and I'm wanting to allow a circle to move down using the down arrow on the keyboard. Here's my code:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y = y+=speed;
}
}
}
}
}
When I test it, nothing happens when I press the down key. Anyone know what's wrong with the code?
Try adding KeyBoard events to the stage instead of to the class. Additionally, I would not nest functions like that, bad practice in general. Also the line y = y+=speed; is confusing, shouldn't it just be y += speed; ?
EDIT: Sorry, I guess stage will be null in the constructor, I've added a ADDED event listener.
Try this:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Circle extends MovieClip {
public function Circle() {
// constructor code
var speed:int = 3;
addEventListener(Event.ADDED, onAdded);
}
private function onAdded(event:Event) {
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyIsDown);
}
private function keyIsDown(event:KeyboardEvent) {
if(event.keyCode == Keyboard.DOWN) {
y += speed;
}
}
}
}