-
as3 newbie.. timer class
hey there today I embarked on the world of cs3 and as3... just trying to build a simple game.. where the users avoids the blocks, it's a good warm up piece, so far i've coded the drag function and the hit test in it's basic form..
i've been having some problems with the timer class, every article i can find refers to it calling a event / function after the time has elapsed, what i'm after is a timer which times up.. giving the user a time survived when they die.
here's the code i have so far.
Code:
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.getTimer;
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0xFFCC00);
circle.graphics.drawCircle(0, 0, 40);
var target1:Sprite = new Sprite();
target1.graphics.beginFill(0xCCFF00);
target1.graphics.drawRect(0, 0, 100, 100);
target1.name = "target1";
var target2:Sprite = new Sprite();
target2.graphics.beginFill(0xCCFF00);
target2.graphics.drawRect(0, 200, 100, 100);
target2.name = "target2";
addChild(target1);
addChild(target2);
addChild(circle);
target1.x = 100;
target2.x = 400;
circle.x = 300;
circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDowner);
function mouseDowner(event:MouseEvent):void {
//
trace("called");
circle.startDrag(true);
circle.addEventListener(Event.ENTER_FRAME,enemyhit);
}
function enemyhit(event:Event):void {
//
if (circle.hitTestObject(target1)) {
timer();
circle.stopDrag();
trace("circle hit target you are dead!");
circle.removeEventListener(Event.ENTER_FRAME,enemyhit);
circle.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDowner);
gameTimer.start();
}
}
function timer() {
var duration:uint = gameTimer.getTimer();
trace("duration:" + duration);
}
function timeEnd(event:TimerEvent):void {
trace("timeEnd: " + event);
}
function mover() {
}
any help would be grand..
Andy
-
The Timer object and TimerEvent event are for the time based event processing that you've read about. What you are looking for is the flash.utils.getTimer method.
Simply store the result of getTimer() when the game starts, and subtract that from the value of getTimer() when the game ends, and you'll get a value in milliseconds which the player survived.
Psuedocode
Code:
private var duration:int;
public function beginGame():void{
duration = getTimer();
//more stuff
}
public function endGame():void{
duration = getTimer() - duration;
trace("you lasted "+(duration/1000)+" seconds");
//more stuff
}
-
Thank you..
Can't thankyou enough.
Was driving me crazy.. you would think they would reference it some where in the doc on how to make a simple session timer.
Migrating to AS3 is fun.. I can see a lot of plus points but also a lot of added fussy bits.. Ok it might make it faster for app dev but still.
I'm still trying to get my head around all this public / package stuff..
how long before it becomes pure java.
Andy.
-
Ok another problem which I don't understand.. I've coded this function to loop through all 4 badies on screen, so I don't have to repeat the function for times..
All i'm getting is .. error number 1120:Access of undefined property target.
when i've got 4 targets on stage?
Code:
function enemyhit(event:Event):void {
//
var nb:int = numBoss
for (var i:int = 0; i<nb; i++) {
if (circle.hitTestObject(target[i])) {
endGame();
trace(target3);
}
}
}
Any ideas?
-
What sort of event is that a listener for? You may have meant "event.target". But I don't think so.
In order for that code to work, you'd have to have a variable named target which is an array of things that can be hitTested against. Also, you need a variable named target3.
I am guessing that this is confusion between variable names and instancenames. This has been covered in depth in this forum before. In fact, here's my little rant about it:
http://board.flashkit.com/board/showthread.php?t=754890
-
ummm
target 1 - 4 are spites generated when the movie loads..
I'm trying to get the the circle hit testing against all four targets using a loop.. i've tried different ways.. maybe I just don't get it.. but flash as2 used to let you loop instance names. here's my whole code..
This is an array example.. also i'm going to try and class package it at some point, is it difficult to do?
Code:
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.getTimer;
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0xFFCC00);
circle.graphics.drawCircle(0, 0, 40);
var target1:Sprite = new Sprite();
target1.graphics.beginFill(0xCCFF00);
target1.graphics.drawRect(0, 0, 100, 100);
target1.name = "target1";
var target2:Sprite = new Sprite();
target2.graphics.beginFill(0xCCFF00);
target2.graphics.drawRect(0, 100, 100, 100);
target2.name = "target2";
var target3:Sprite = new Sprite();
target3.graphics.beginFill(0xCCFF00);
target3.graphics.drawRect(0, 200, 100, 100);
target3.name = "target3";
var target4:Sprite = new Sprite();
target4.graphics.beginFill(0xCCFF00);
target4.graphics.drawRect(0, 300, 100, 100);
target4.name = "target4";
//add to stage
//
addChild(target1);
addChild(target2);
addChild(target3);
addChild(target4);
addChild(circle);
//place x value
//
target1.x = 100;
target2.x = 200;
target3.x = 300;
target4.x = 400;
circle.x = 500;
//set duration
//
var duration:int;
//Start Game Listener
//
circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDowner);
//Start Game Action
//
function mouseDowner(event:MouseEvent):void {
//
beginGame();
//
}
//Begin Game Function - Starts all events.
//
function beginGame():void {
//
trace("called");
circle.startDrag(true);
circle.addEventListener(Event.ENTER_FRAME,enemyhit);
duration = getTimer();
//more stuff
}
//Enemy Hit test - Tests to see if hero hits any other objects
//
function enemyhit(event:Event):void {
//
var targetArray:Array = ["target1", "target2", "target3", "target4"];
//
for (var i:String in targetArray) {
if (circle.hitTestObject(targetArray[i])) {
endGame();
}
}
}
//End Game Function - Finishes the game
//
function endGame() {
//
circle.stopDrag();
circle.removeEventListener(Event.ENTER_FRAME,enemyhit);
circle.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDowner);
duration = getTimer() - duration;
trace("circle hit target you are dead! You lasted : " + duration);
}
with this method all i'm getting is error 1034 : Type coercion failed.. cannot convert "target1" to flash.display.DisplayObject at game_basic_code.fla::MainTimeline/enemyhit();
Sorry about being a pain in the ass.. nice little rant btw, kinda reminds me when I used to try others with AS2.
-
Yup, instancename vs variable confusion. You have "target1", you want target1. The first one is a String, the second is a Sprite.
Just change your array and how you're looping through it:
Code:
var targetArray:Array = [target1, target2, target3, target4];
//
for (var i:int = 0; i < targetArray.length; i++) {
if (circle.hitTestObject(targetArray[i])) {
endGame();
}
}
-
thanks for that, woud it be quicker to reference them to the number rather than storing them in an array.
as in
Code:
function enemyhitB(event:Event):void {
//
//
for (var i:int;i<5;i++) {
if (circle.hitTestObject(target[i])) {
endGame();
}
}
}
how would I get the var name without having to use an array or is this impossible? so i could have more enemys added without having to add them to the named array.
I plan to generate the enemies using another loop
Andy.
-
What you have there won't work, because there is no target array. target[1] references the object at index 1 in the target array. It does NOT automatically mean target1.
If you create your enemies with a loop (and I think that's the way to go), simply add them to your targetArray at creation time.
-
And how in as3 would I go about doing that.
Code:
var numEnemys = 4
var xpos = 0;
var ypos = 0;
var targetArray:Array = [];
for (var i:int; i<numEnemys; i++) {
var target[i]:Sprite = new Sprite();
target[i].graphics.beginFill(0xCCFF00);
target[i].graphics.drawRect(0, 0, 100, 100);
target[i}.name = "target1";
target[i].x = xpos*i
target[i].y = ypos*i
targetArray[i] = target[i];
}
I'm totally confused .. ummm.
Also is it better to use MC instead of sprites.. sprites remind me of my old lingo days.
I aim to replace these drawn elements with Movie Clips from the lib, i was just using blocks as a starting point to get the physics and interaction right.
Thanks for all you help so far.. it's been an eye opener to the way in which as3 works.
Andy.
-
You're actually pretty close.
Code:
var numEnemys = 4
var xpos = 0;
var ypos = 0;
var targetArray:Array = [];
for (var i:int; i<numEnemys; i++) {
var target:Sprite = new Sprite();
target.graphics.beginFill(0xCCFF00);
target.graphics.drawRect(0, 0, 100, 100);
//target.name = "target1"; //you don't need this! Yaaay!
target.x = target.width*i + 5;
target.y = ypos*i
targetArray.push(target);
}
I also changed the placement so they don't all go to 0, 0.
-
hey thanks for that..
Only problem with that method is how on earth do i get the pieces in different places.
Or do I have to move them after they have been generated..
I guess the best way to do that would be to loop through the array and asign each piece to it's new x and y.
It's starting point.
well that's for tomorrow, when I get the pieces to move..
Thankyou for all your help I will try and get on with it on my own for now and if i get really stuck then give u a bell if that's ok.. i don't like being a burden.
If your ever in Central London I ow you a beer or two..
Andy.
-
FYI
This is the game i'm trying to construct in flash as3.. thought it would be a nice easy task to get me into as3
Ugly Box Game
Much thanks
-
Update.. moving boxes.
Hey there.. thought I would give you guys an update.
After playing about a bit i've managed to make my boxes hit test all of the walls.. although I would of perfered to do this dynamically on the size and height of the stage, I couldn't get that to work so ended up drawing walls on the stage.
As for the movement of the boxes, I'm not quite sure what i'm doing.. I;ve got them to move but they are too uniform for my liking i've tried playing around with Math.random but this seems to make no difference.
I'm sure i've prob done it the long way around but still it seems to work.
any tips and pointers would be grand, I'm getting there slowly.
Here's the code for anyone who would like to follow what i've been up to.
Code:
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.getTimer;
import flash.text.*;
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0xFFCC00);
circle.graphics.drawRect(0, 0, 40,40);
var numEnemys = 4;
var ypos = 0;
var xpos = 30;
var targetArray:Array = [];
var posXArray:Array = [0,300,0,300];
var posYArray:Array = [0,0,300,300];
var dirX:Array = [1,1,1,1];
var dirY:Array = [1,1,1,1];
var col:Array = [0xCCFF00,0xFF0000,0x6699CC,0xFF66FF];
var speedX:Array =[5,5,5,5]
var speedY:Array =[5,5,5,5]
var walls:Array = [wall1,wall2,wall3,wall4];
var a:int = 0;
for (var i:int; i<numEnemys; i++) {
var target:Sprite = new Sprite();
target.graphics.beginFill(col[i]);
target.graphics.drawRect(0, 0,100,100);
//target.x = target.width*i + 5;
target.x = posXArray[i];
target.y = posYArray[i];
//target.y = ypos*i;
targetArray.push(target);
addChild(target);
trace(targetArray);
trace(target.x);
}
//add to stage
//
addChild(circle);
circle.x = 550/2;
circle.y = 200;
//place x value
//set duration
//
var duration:int;
var playing = true;
//Start Game Listener
//
circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDowner);
circle.addEventListener(MouseEvent.MOUSE_UP,mouseUnClick);
//Start Game Action
//
function mouseDowner(event:MouseEvent):void {
//
circle.startDrag(false);
if (playing) {
trace("fresh Game");
beginGame();
playing = false;
}
//
}
function mouseUnClick(event:MouseEvent):void {
//
circle.stopDrag();
//
}
//Begin Game Function - Starts all events.
//
function beginGame():void {
//
trace("called");
circle.addEventListener(Event.ENTER_FRAME,playGame);
duration = getTimer();
//more stuff
}
function playGame(event:Event):void {
//Display time.
//Not sure how to do this in AS3 keep getting error 1067 : Implicit coercion of a value of type int to urelated type string..
//var timetext:String = duration;
//time.text = timetext
for (var i:String in targetArray) {
//Moves the X axis
if (dirX[i] == 1) {
targetArray[i].x = posXArray[i]+= speedX[i];
} else if (dirX[i] == 0) {
targetArray[i].x = posXArray[i]-= speedX[i];
}
//Moves the Y axis
if (dirY[i] == 1) {
targetArray[i].y = posYArray[i]+= speedY[i];
} else if (dirY[i] == 0) {
targetArray[i].y = posYArray[i]-= speedY[i];
}
if (targetArray[i].hitTestObject(wall1)) {
if (dirX[i]== 1) {
dirX[i] = 0;
speedY[i]+=0.5
} else if (dirX[i] == 0) {
dirX[i] = 1;
speedX[i]+=0.5
}
}
if (targetArray[i].hitTestObject(wall2)) {
if (dirY[i]== 1) {
dirY[i] = 0;
speedY[i]+=0.2;
} else if (dirY[i] == 0) {
dirY[i] = 1;
}
}
if (targetArray[i].hitTestObject(wall3)) {
if (dirX[i]== 1) {
dirX[i] = 0;
speedY[i]+=0.2;
} else if (dirX[i] == 0) {
dirX[i] = 1;
speedY[i] = 0.3;
}
}
if (targetArray[i].hitTestObject(wall4)) {
if (dirY[i]== 1) {
dirY[i] = 0;
speedY[i]+=0.5;
} else if (dirY[i] == 0) {
dirY[i] = 1;
speedX[i]+=1.5;
}
}
if (circle.hitTestObject(targetArray[i])) {
endGame();
}
}
}
//End Game Function - Finishes the game
//
function endGame() {
//
circle.stopDrag();
circle.removeEventListener(Event.ENTER_FRAME,playGame);
circle.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDowner);
duration = getTimer() - duration;
trace("circle hit target you are dead! You lasted : " + duration);
}
There are four walls on the stage and text box which I can't get to display in as3
-
The game so far - code and and a question.
Quick update and a call for help
Right I've now got a fully working small game, but my problem is now that I can't seem to get Duration to display in a text field within the movie clip score_girl_mc
It's a bit annoying as i've tried everything I've read about, short of dynamically generating the text field, which I don't want to do as the idea is to keep it styled up in it's parent clip.
So can anyone help me here? also I know my code is not normal as3 as in extends etc, i would like to do this but not sure where I would start.. and really don't know if it would stop my whole movie from functioning.
Andy.
Code:
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.getTimer;
import flash.text.*;
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0xFFCC00);
circle.graphics.drawRect(0, 0, 40,40);
var numEnemys = 4;
var ypos = 0;
var xpos = 30;
var targetArray:Array = [];
var posXArray:Array = [25,390,25,326,50];
var posYArray:Array = [25,25,300,328,290];
var dirX:Array = [1,1,1,1,1];
var dirY:Array = [1,1,1,1,1];
var col:Array = [0xCCFF00,0xFF0000,0x6699CC,0xFF66FF,0xFF66FF];
var sizeH:Array = [72,110,35,72,72];
var sizeW:Array = [120,120,120,120,120];
var speedX:Array =[6,5,7,5,8];
var speedY:Array =[5,7,5,8,5];
var walls:Array = [wall1,wall2,wall3,wall4];
var bosses:Array = [boss1_mc,boss3_mc,boss4_mc,boss2_mc,boss1_mc];
var a:int = 0;
for (var i:int; i<numEnemys; i++) {
var target:Sprite = new Sprite();
//target.graphics.beginFill(col[i]);
//target.graphics.drawRect(0, 0,sizeH[i],sizeW[i]);
target.x = target.width*i + 5;
target.x = posXArray[i];
target.y = posYArray[i];
//target.y = ypos*i;
targetArray.push(target);
addChild(target);
target.addChild(new bosses[i]);
}
//add to stage
//
addChild(circle);
circle.x = 480/2;
circle.y = 240;
//place x value
//set duration
//
var duration:int;
var playing = true;
//Start Game Listener
//
circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDowner);
circle.addEventListener(MouseEvent.MOUSE_UP,mouseUnClick);
//Start Game Action
//
function mouseDowner(event:MouseEvent):void {
//
circle.startDrag(false);
if (playing) {
trace("fresh Game");
beginGame();
playing = false;
}
//
}
function mouseUnClick(event:MouseEvent):void {
//
circle.stopDrag();
//
}
//Begin Game Function - Starts all events.
//
function beginGame():void {
//
trace("called");
circle.addEventListener(Event.ENTER_FRAME,playGame);
duration = getTimer();
//more stuff
}
function playGame(event:Event):void {
//Display time.
//Not sure how to do this in AS3 keep getting error 1067 : Implicit coercion of a value of type int to urelated type string..
//var timetext:String = duration;
//time.text = timetext
for (var i:String in targetArray) {
//Moves the X axis
if (dirX[i] == 1) {
targetArray[i].x = posXArray[i]+= speedX[i];
} else if (dirX[i] == 0) {
targetArray[i].x = posXArray[i]-= speedX[i];
//targetArray[i].gotoAndPlay(2);
}
//Moves the Y axis
if (dirY[i] == 1) {
targetArray[i].y = posYArray[i]+= speedY[i];
} else if (dirY[i] == 0) {
targetArray[i].y = posYArray[i]-= speedY[i];
}
if (targetArray[i].hitTestObject(wall1)) {
if (dirX[i]== 1) {
dirX[i] = 0;
speedY[i]+=0.5;
} else if (dirX[i] == 0) {
dirX[i] = 1;
speedX[i]+=0.5;
}
}
if (targetArray[i].hitTestObject(wall2)) {
if (dirY[i]== 1) {
dirY[i] = 0;
speedY[i]+=0.2;
} else if (dirY[i] == 0) {
dirY[i] = 1;
}
}
if (targetArray[i].hitTestObject(wall3)) {
if (dirX[i]== 1) {
dirX[i] = 0;
speedY[i]+=0.2;
} else if (dirX[i] == 0) {
dirX[i] = 1;
speedY[i] = 0.5;
}
}
if (targetArray[i].hitTestObject(wall4)) {
if (dirY[i]== 1) {
dirY[i] = 0;
speedY[i]+=0.5;
} else if (dirY[i] == 0) {
dirY[i] = 1;
speedX[i]+=1.5;
}
}
if (circle.hitTestObject(walls[i])) {
//endGame();
}
if (circle.hitTestObject(targetArray[i])) {
endGame();
}
}
}
//End Game Function - Finishes the game
//
function endGame() {
//
circle.stopDrag();
circle.removeEventListener(Event.ENTER_FRAME,playGame);
circle.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDowner);
//Get Duration For Text Box
duration = getTimer() - duration;
//
//Build and display score girl
var scoretag:MovieClip = new MovieClip();
addChild(scoretag);
scoretag.addChild(new score_girl_mc)
scoretag.x = 30;
scoretag.y = 5;
scoretag.visible = true;
//trace out duration
trace("circle hit target you are dead! You lasted : " + duration);
//scoretag.score_girl_mc.score_txt.text = duration;
}
-
Is it this that you're trying to fix?
Code:
//Display time.
//Not sure how to do this in AS3 keep getting error 1067 : Implicit coercion of a value of type int to urelated type string..
//var timetext:String = duration;
//time.text = timetext
It's right. You're trying to put a number where it expects a string. You can do this:
Code:
//Display time.
var timetext:String = ""+duration;
time.text = timetext
When you add a number to an existing string, it gets converted to a string. There's probably a more "correct" method involving Number and toString(), but I usually don't bother with it.
-
thankyou for that..
I'm learning something new everyday.. well when ever you reply didn't realise as3 was so picky something i'm quickly learning as it often doesn't let you compile without everything working and referencing etc etc.
This is what i'm trying to do, for some reason i can't seem to reference my text field using the standard movie clip path method.
I'm getting the error
Code:
TypeError: Error #1010: A term is undefined and has no properties.
at game_basic_code_fla::MainTimeline/endGame()
at game_basic_code_fla::MainTimeline/playGame()
Code:
var scoretag:MovieClip = new MovieClip();
addChild(scoretag);
scoretag.addChild(new score_girl_mc)
scoretag.x = 30;
scoretag.y = 5;
scoretag.visible = true;
//trace out duration
trace("circle hit target you are dead! You lasted : " + duration);
//Path to the movie
scoretag.score_girl_mc.score_txt.text = timetext;
Mucho thanko
-
This particular issue seems to get everyone who comes from as2 to as3. InstanceNames are NOT properties, or variables, or anything but strings attached to objects.
Specifically, this:
Code:
scoretag.addChild(new score_girl_mc());
does NOT create
Code:
scoretag.score_girl_mc
If your score_girl_mc class is defined to have a score_txt property which is a TextField, you can do this:
Code:
var scoretag:MovieClip = new MovieClip();
addChild(scoretag);
var sgmc:score_girl_mc = new score_girl_mc();
scoretag.addChild(sgmc)
scoretag.x = 30;
scoretag.y = 5;
scoretag.visible = true;
//trace out duration
trace("circle hit target you are dead! You lasted : " + duration);
sgmc.score_txt.text = timetext;
-
Cool thanks.. only problem I now get an Syntax error : extra characters found after the end of the program.
Funny thing is it works when i remove that last chunk of code, no syntax errrors.. how bizare.
Not loving as3, I thought things where meant to get simplier as they evolve lol
-
Those extra characters are probably due to extra closing "}" or some missing further up the file.
Not loving as3, I thought things where meant to get simplier as they evolve lol
Nope, not necessarily simpler, just more elegant.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|