I have a basic game function that drops a ball every 500 milliseconds (with a timer event) and suppose to get rid of it when it hits a plate that drags after the mouse. The thing is; I tried to use the deleteChild(); function, but it only removes the visual appearance of the ball object’ without stopping its functionality, which causes a couple of problems:
- The ball keeps going, and triggers an event of hitting the floor.
- The dropBall(); function which animates the dropping ball, doesn’t actually animate a new ball.
this is the complete script:
//imports:
import flash.events.Event;
import fl.transitions.Tween;
import flash.text.TextField;
import flash.utils.Timer;
import flash.events.TimerEvent;
//the function 'startGame, handles all basic game functions.
function startGame() {
//inisializes the variable 'Score' and gives it a value of 0.
var Score = 0;
//enter_frame listener. calls 'movePlate'
stage.addEventListener(Event.ENTER_FRAME, movePlate);
//function 'movePlate'. makes the plate follow the mouse
function movePlate(event:Event):void {
plate.x = mouseX;
}
//calls the dropBall function
dropBall()
//the function 'dropBall'. animates the droping ball
function dropBall() {
var oldpost = 0;
var randomNum:Number = Math.random() * 550;
var xAxis:int = Math.round(randomNum);
trace(randomNum);
trace(xAxis);
ball.x = xAxis;
base.x = xAxis;
var oldpost = xAxis;
var ballTween:Tween = new Tween(ball, "y", null, 0, 500, 1.2, true);
oldpost = xAxis;
}
//function 'gameTime'. the timer function that controlls the intervals between falling eggs and ratio of good to bad eggs.
var gameTime1:Timer = new Timer(1000);
gameTime1.addEventListener(TimerEvent.TIMER, gameTimer1Function)
function gameTimer1Function(evt:TimerEvent):void {
dropBall();
}
gameTime1.start();
//enter frame event listener. calls 'checkCollision'
addEventListener(Event.ENTER_FRAME,checkCollision);
//function checl collision. checks if the ball hits the plate
function checkCollision(event: Event):void {
if(ball.hitTestObject(plate)) collisionDetected();
}
//function collision detected
function collisionDetected():void {
Score ++;
trace(Score);
scoreText.text = Score;
}
//enter frame event listener. calls 'checkGameOver'.
addEventListener(Event.ENTER_FRAME,checkGameOver);
//function 'checkGameOver.
function checkGameOver(event: Event):void {
if(ball.hitTestObject(floor)) gameOver();
}
//function 'gameOver'.
function gameOver():void {
trace('GAME OVER!!! Your Score Is: ' + Score + '.');
trace('Asta la Vista Baby :D');
}
}
startGame();
Your’e code is not working as you explained. It is working like this:
You have only one ball, and it is beeing thorwn up, every second, refering to
gameTime1tick calls dropBall (), which doing the following:var ballTween:Tween = new Tween(ball, "y", null, 0, 500, 1.2, true);UPDATE:
For how to export the movieclipt int oclass follow the tutorial:
http://www.kirupa.com/developer/flashcs3/movieclips_classes_AS3_pg1.htm
Then in your logics:
Function ( createBall )
Should look like this:
increase...anddecrease..after they done everything should beremoveBallfunction, and inincrease..then a call to thecreateBallso everything is starting to repeat.P.S. No offence please, but you can learn the programming language, in this case i see you know a bit, but if you are missing some logics, you will not find it on forums… Read some basics info about logics / architectures of the applications etc… and please take a look how the instances needed to be handeled.
P.S.S. Invest some to to know wtf is OOP. 😉
best wishes! Happy holidays 🙂