#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    38
    Rep Power
    4

    Exclamation Flash based Tetris


    So I've been trying to learn a few languages this summer and ended up learning all the basics and nothing really in depth. I decided I needed to make something worthwhile.

    One of my main interests is playing games so I thought why not make a game? I see lots on the Internet games based in Flash so I thought that would be my best starting point to make actual graphic based games... (not to mention I actually got taught at university how to use flash to a certain extent).

    The game I decided upon was tetris. I'm sure you're all familiar with the block based puzzle game and might be able to shed some light upon what I am doing right and what I am doing wrong.

    So, I've firstly made my game screen with a window for all the blocks to come down in. I've also put some dynamic text place holders ready for when I come to keeping track of how many 'lines' the player has scored, the time and so on in the right hand side of the screen.

    I have made graphics for the different colour 'building blocks' (just one block of 24x24 in different colours for each shape) and then used the 'building blocks' multiple times to create my shapes (I, J, L, O, S, T, Z). These blocks are called tetrominoes.

    Now I've come to a point where I need to import these tetrominoes randomly from the library and get them to appear in the middle just off the top of my block screen.
    That is my first question and Google provides no answers:

    How do I import random objects from a particular folder in my library to the stage in Flash CS5 AS3?

    So, that's my first question...

    After realising a quick solution to my problem was not to be found quickly I then thought about what will happen to this block once it has been imported... Well, in Tetris the block moves slowly down until it hits another block or the bottom of the block screen.

    I know that I can do something with a detectHit() function so before I ask for help on that I will experiment myself (which requires the above part of my game to work first) but my second question for this post is:

    How do I get my block to move down the screen by a set amount of pixels every second or so?

    Thanks in advanced for any help you may be able to provide...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    38
    Rep Power
    4

    update


    To keep you all updated.

    I have now worked out how to get all my tetrominoes onto the stage. I still would love to know how to get each one to be activated randomly to make the game different each time you play it.

    Also, my code will follow as I am no getting error: Scene 1, Layer 'actions', Frame 1, Line 22 1061: Call to a possibly undefined method addEventListener through a reference with static type Class.

    Actionscript 3
    Code:
     	import flash.display.MovieClip;
    	import flash.utils.Timer;
    	import flash.events.MouseEvent;
            
            // adding main menu
    	var redbgInstance:redbg = new redbg
    	var tetris_txtInstance:tetris_txt = new tetris_txt
    	var play_btnInstance:play_btn = new play_btn
    	var quit_btnInstance:quit_btn = new quit_btn
    	addChild(redbgInstance);
    	addChild(play_btnInstance);
    	addChild(quit_btnInstance);
    	addChild(tetris_txtInstance);
    	redbgInstance.x = stage.stageWidth / 2;
    	redbgInstance.y = stage.stageHeight / 2;
    	play_btnInstance.x = stage.stageWidth / 2;
    	play_btnInstance.y = stage.stageHeight / 2;
    	quit_btnInstance.x = stage.stageWidth / 2;
    	quit_btnInstance.y = 250;
    	tetris_txtInstance.x = stage.stageWidth / 2;
    	tetris_txtInstance.y = 120;
    	
    	play_btn.addEventListener(MouseEvent.CLICK, playGame);
    	function playGame(e:MouseEvent):void {
    		// removing main menu to reveal game behind
                    removeChild(redbgInstance);
    		removeChild(play_btnInstance);
    		removeChild(quit_btnInstance);
    	}
    	
    
    		// making the user gameplay data to be altered dynamically as game goes on
    		var userLines:int = 000;
    		var userLevel:int = 0;
    		
    		// setting what level the user will be on dependent on the amount
    		// of lines they have completed
    		if (userLines > 20 && userLines <0){
    			userLevel = 1;
    		} else if (userLines > 40 && userLines < 20){
    			userLevel = 2;
    		} else if (userLines > 60 && userLines < 40){
    			userLevel = 3;
    		} else if (userLines > 80 && userLines < 60){
    			userLevel = 4;
    		} else if (userLines > 100 && userLines < 80){
    			userLevel = 5;
    		}
    		
    		// making the timer so that the objects will move later
    		// this will also be displayed in the 'time' dynamic text
    		var myTimer:Timer = new Timer(1000,99999999999);
    		myTimer.addEventListener(TimerEvent.TIMER, timerListener);
    		function timerListener (e:TimerEvent):void{
    		trace ("Timer trigger")
    		}
    		// making our tetrominoes appear and in the right place
    		// tetrominoes should appear off screen to begin with
    		// using the timer later i will make them move
    		
    		
    		var blockIInstance:blockI = new blockI
    		addChild(blockIInstance);
    		blockIInstance.x = 188.2;
    		blockIInstance.y = -102.8;
    		
    		
    		// the J block
    		var blockJInstance:MovieClip = new blockJ
    		addChild(blockJInstance);
    		blockJInstance.x = 188.2;
    		blockJInstance.y = -102.8;
    		
    		// the L block
    		var blockLInstance:MovieClip = new blockL
    		addChild(blockLInstance);
    		blockLInstance.x = 188.2;
    		blockLInstance.y = -102.8;
    		
    		// the O block
    		var blockOInstance:MovieClip = new blockO
    		addChild(blockOInstance);
    		blockOInstance.x = 188.2;
    		blockOInstance.y = -102.8;
    		
    		// the S block
    		var blockSInstance:MovieClip = new blockS
    		addChild(blockSInstance);
    		blockSInstance.x = 188.2;
    		blockSInstance.y = -102.8;
    		
    		// the T block
    		var blockTInstance:MovieClip = new blockT
    		addChild(blockTInstance);
    		blockTInstance.x = 188.2;
    		blockTInstance.y = -102.8;
    	
    		// the Z block
    		var blockZInstance:MovieClip = new blockZ
    		addChild(blockZInstance);
    		blockZInstance.x = 188.2;
    		blockZInstance.y = -102.8;
    also another thing that im struggling with being able to dynamically add an instance name to movie clips that i add dynamically... any help on this will be appreciated...
    nevermind i worked this bit out... instance names aren't needed in AS3 code still getting my head around this (evidently)
    Last edited by bowers_1991; July 24th, 2011 at 08:27 AM. Reason: extra question
  4. #3
  5. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    942
    Rep Power
    96
    To randomize the pieces, I'd do something like this (assuming your pieces are in symbols in the library):

    Code:
    var randomPiece=Math.floor(Math.random()*(1+High-Low))+Low;
    switch (randomPiece) {
       case 1 : var newPiece=new pieceOne(); break;
       case 2 : var newPiece=new pieceTwo(); break;
       case 3 : var newPiece=new pieceThree(); break;
    }
    
    addChild(newPiece);

    Check your code:

    Code:
    var play_btnInstance:play_btn = new play_btn
    var quit_btnInstance:quit_btn = new quit_btn
    should be:

    Code:
    var play_btnInstance:play_btn = new play_btn()
    var quit_btnInstance:quit_btn = new quit_btn()
    Just missing the parens at the end. Maybe that will fix your error. If not, I'm not seeing a play_btn object anywhere else in your code. I see "play_btnInstance" instead.

    Instance names are still used in AS3! Very important!

    I don't use MC's much in AS3; Sprites are the new Movie Clip for me simply because of the lower overhead. To add an instance name to dynamically-generated sprite (or MC):

    var cow=new Sprite();
    cow.name="this is my instance name";

    That simple!
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    38
    Rep Power
    4
    Originally Posted by Frank Grimes
    var cow=new Sprite();
    cow.name="this is my instance name";
    !
    is this to say that you can't give an MC an instance name.
    I came across this name property but it didn't really work with my move clips... maybe i was doing it wrong but if the above is true then it might shed some light and provide new knowledge to a guy that has only been doing this AS3 for a few days

    cheers
  8. #5
  9. Plays with fire
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2003
    Location
    Barsoom
    Posts
    942
    Rep Power
    96
    You can name MovieClips. It works the same way.
    “Be ashamed to die until you have won some victory for humanity.” -- Horace Mann

    "...all men are created equal." -- US Declaration of Independence

IMN logo majestic logo threadwatch logo seochat tools logo