A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Displaying Score

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5

    Displaying Score

    Once again I'm having trouble with this code :[

    import flash.events.Event;
    import flash.display.Sprite;
    import flash.utils.Timer;

    stop();
    var tobpos:int;
    var falling:Sprite;
    var rising:Sprite;
    var dropTimer:Timer;
    var dropInterval:uint = 500;
    var dropAmount:uint = 10;
    var fallSpeed:uint = 5;
    var Lives:uint = 0;

    var nl1: lifeone = new lifeone;
    stage.addChild(nl1);
    nl1.x = 225;
    nl1.y =575;
    var nl2: lifetwo = new lifetwo;
    stage.addChild(nl2);
    nl2.x = 280;
    nl2.y =575;
    var nl3: lifethree = new lifethree;
    stage.addChild(nl3);
    nl3.x = 340;
    nl3.y =575;

    var outputText:TextField = new TextField();


    initGame();

    function initGame():void {
    tobpos = toby.x;
    rising = new Sprite();
    addChild(rising);
    falling = new Sprite();
    addChild(falling);
    dropTimer = new Timer(dropInterval,dropAmount);
    dropTimer.start();
    dropTimer.addEventListener(TimerEvent.TIMER, make_berry);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, make_balloon);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, toby_Moving);
    stage.addEventListener(Event.ENTER_FRAME, chk_hits);


    }
    function chk_hits(e:Event):void {
    for (var i:uint=0; i<rising.numChildren; i++) {
    for (var j:uint=0; j<falling.numChildren; j++) {
    if (rising.getChildAt(i).hitTestObject(falling.getChi ldAt(j))) {
    falling.getChildAt(j).removeEventListener(Event.EN TER_FRAME, do_falling);
    falling.removeChild(falling.getChildAt(j));
    rising.getChildAt(i).removeEventListener(Event.ENT ER_FRAME, do_rising);
    rising.removeChild(rising.getChildAt(i));
    var nplusone: plusone = new plusone;
    trace("hit one");
    stage.addChild(nplusone);
    nplusone.x = stage.stageWidth / 2;
    nplusone.y = stage.stageHeight / 3;
    break;
    }
    }
    }
    }
    function toby_Moving(event:KeyboardEvent):void {
    switch (event.keyCode) {
    case Keyboard.LEFT :
    toby.x -= 25;
    tobpos = toby.x;
    break;
    case Keyboard.RIGHT :
    toby.x += 25;
    tobpos = toby.x;
    break;
    default :
    break;
    }
    }
    function make_berry(e:Event=null) {
    var ranx:int = Math.round(Math.random() * stage.stageWidth);
    var nstrawberry:strawberry = new strawberry();
    falling.addChild(nstrawberry);
    nstrawberry.x = ranx;
    nstrawberry.y = stage.stageHeight - 800;
    nstrawberry.addEventListener(Event.ENTER_FRAME, do_falling);
    }
    function do_falling(e:Event):void {
    e.currentTarget.y += fallSpeed;
    if (e.currentTarget.y > 450) {
    e.currentTarget.removeEventListener(Event.ENTER_FR AME, do_falling);
    e.currentTarget.parent.removeChild(DisplayObject(e .currentTarget));
    var nminusone: minusone = new minusone();
    stage.addChild(nminusone);
    nminusone.x = stage.stageWidth / 2;
    nminusone.y = stage.stageHeight / 3;
    Lives++;

    switch(Lives)
    {
    case 1:
    stage.removeChild(nl3);

    break;

    case 2:

    stage.removeChild(nl2);

    break;

    case 3:

    stage.removeChild(nl1);

    break;

    default:
    break;
    }

    }
    }
    function make_balloon(event:KeyboardEvent):void {
    switch (event.keyCode) {
    case Keyboard.SPACE :
    var nballoon:bballoon = new bballoon();
    rising.addChild(nballoon);
    nballoon.x = tobpos;
    nballoon.y = stage.stageHeight / 2;
    nballoon.addEventListener(Event.ENTER_FRAME, do_rising);
    break;
    default :
    break;
    }
    }
    function do_rising(e:Event):void {
    e.currentTarget.y -= 10;
    if (e.currentTarget.y < 0) {
    e.currentTarget.removeEventListener(Event.ENTER_FR AME, do_rising);
    e.currentTarget.parent.removeChild(DisplayObject(e .currentTarget));
    }
    }
    I want to make it so that:

    var nminusone: minusone = new minusone();
    stage.addChild(nminusone);
    nminusone.x = stage.stageWidth / 2;
    nminusone.y = stage.stageHeight / 3;
    disappears off the screen after 2 seconds and to be able to keep score/display score for everytime a berry hits the floor.

    Apparently Actionscript 3.0 doesnt support _root and it's killing me :[

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.utils.Timer;
    import flash.display.DisplayObject;
    import fl.transitions.*;
    import fl.transitions.easing.*;
    
    stop();
    var tobpos:int;
    var falling:Sprite;
    var rising:Sprite;
    var dropTimer:Timer;
    var dropInterval:uint = 500;
    var dropAmount:uint = 10;
    var fallSpeed:uint = 5;
    var numLives:uint = 0;
    var panel:Sprite;
    var score:scorebox;
    
    init_game();
    
    function init_game():void {
    	tobpos = toby.x;
    	rising = new Sprite();
    	addChild(rising);
    	falling = new Sprite();
    	addChild(falling);
    	dropTimer = new Timer(dropInterval,dropAmount);
    	dropTimer.start();
    	dropTimer.addEventListener(TimerEvent.TIMER, make_berry);
    	stage.addEventListener(KeyboardEvent.KEY_DOWN, make_balloon);
    	stage.addEventListener(KeyboardEvent.KEY_DOWN, toby_moving);
    	stage.addEventListener(Event.ENTER_FRAME, chk_hits);
    
    	panel = new Sprite();
    	panel.y = 575;
    	addChild(panel);
    	var nl1:lifeone = new lifeone();
    	nl1.x = 225;
    	panel.addChild(nl1);
    	var nl2:lifetwo = new lifetwo();
    	nl2.x = 280;
    	panel.addChild(nl2);
    	var nl3:lifethree = new lifethree();
    	nl3.x = 340;
    	panel.addChild(nl3);
    
    	score = new scorebox();
    	score.y = 575;
    	score.x = 400;
    	score.amt = 0;
    	score.val.text = String(score.amt);
    	addChild(score);
    
    }
    function chk_hits(e:Event):void {
    	for (var i:uint=0; i<rising.numChildren; i++) {
    		for (var j:uint=0; j<falling.numChildren; j++) {
    			if (rising.getChildAt(i).hitTestObject(falling.getChildAt(j))) {
    				falling.getChildAt(j).removeEventListener(Event.ENTER_FRAME, do_falling);
    				falling.removeChild(falling.getChildAt(j));
    				rising.getChildAt(i).removeEventListener(Event.ENTER_FRAME, do_rising);
    				rising.removeChild(rising.getChildAt(i));
    				update_score(10);
    				do_points("plus");
    				break;
    			}
    		}
    	}
    }
    function do_points(typ:String):void {
    	var obj:DisplayObject;
    	switch (typ) {
    		case "plus" :
    			obj = new plusone();
    			break;
    		case "minus" :
    			obj = new minusone();
    			break;
    	}
    	stage.addChild(obj);
    	obj.x = stage.stageWidth / 2;
    	obj.y = stage.stageHeight / 3;
    	var twObj = new Tween(obj,"scaleX",Regular.easeInOut,1,2,0.5,true);
    	new Tween(obj,"scaleY",Regular.easeInOut,1,2,0.5,true);
    	twObj.addEventListener(TweenEvent.MOTION_FINISH, fade_out);
    }
    function fade_out(e:Event):void {
    	var twObj = new Tween(e.currentTarget.obj,"alpha",Regular.easeInOut,1,0,1,true);
    	twObj.addEventListener(TweenEvent.MOTION_FINISH, clean_tween);
    }
    function clean_tween(e:Event):void {
    	stage.removeChild(e.currentTarget.obj);
    }
    function toby_moving(event:KeyboardEvent):void {
    	switch (event.keyCode) {
    		case Keyboard.LEFT :
    			toby.x -=  25;
    			tobpos = toby.x;
    			break;
    		case Keyboard.RIGHT :
    			toby.x +=  25;
    			tobpos = toby.x;
    			break;
    		default :
    			break;
    	}
    }
    function make_berry(e:Event=null):void {
    	var ranx:int = Math.round(Math.random() * stage.stageWidth);
    	var nstrawberry:strawberry = new strawberry();
    	falling.addChild(nstrawberry);
    	nstrawberry.x = ranx;
    	nstrawberry.y = stage.stageHeight - 800;
    	nstrawberry.addEventListener(Event.ENTER_FRAME, do_falling);
    }
    function update_score(pts:Number):void {
    	score.amt +=  pts;
    	score.val.text = String(score.amt);
    }
    function do_falling(e:Event):void {
    	e.currentTarget.y +=  fallSpeed;
    	if (e.currentTarget.y > 450) {
    		e.currentTarget.removeEventListener(Event.ENTER_FRAME, do_falling);
    		e.currentTarget.parent.removeChild(DisplayObject(e.currentTarget));
    		update_score(-10);
    		do_points("minus");
    		numLives++;
    		if (panel.numChildren > 0) {
    			panel.removeChild(panel.getChildAt(0));
    		}
    	}
    }
    function make_balloon(event:KeyboardEvent):void {
    	switch (event.keyCode) {
    		case Keyboard.SPACE :
    			var nballoon:bballoon = new bballoon();
    			rising.addChild(nballoon);
    			nballoon.x = tobpos;
    			nballoon.y = stage.stageHeight / 2;
    			nballoon.addEventListener(Event.ENTER_FRAME, do_rising);
    			break;
    		default :
    			break;
    	}
    }
    function do_rising(e:Event):void {
    	e.currentTarget.y -=  10;
    	if (e.currentTarget.y < 0) {
    		e.currentTarget.removeEventListener(Event.ENTER_FRAME, do_rising);
    		e.currentTarget.parent.removeChild(DisplayObject(e.currentTarget));
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center