Flash Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignFlash Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 23rd, 2011, 07:47 AM
bowers_1991 bowers_1991 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 38 bowers_1991 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 38 m 47 sec
Reputation Power: 2
Exclamation ActionScript 3 - 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...

Reply With Quote
  #2  
Old July 24th, 2011, 07:43 AM
bowers_1991 bowers_1991 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 38 bowers_1991 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 38 m 47 sec
Reputation Power: 2
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

Reply With Quote
  #3  
Old July 28th, 2011, 01:35 PM
Frank Grimes's Avatar
Frank Grimes Frank Grimes is offline
Plays with fire
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Location: Outside looking in
Posts: 894 Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 4 h 40 m 15 sec
Reputation Power: 95
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

Reply With Quote
  #4  
Old July 31st, 2011, 09:06 PM
bowers_1991 bowers_1991 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 38 bowers_1991 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 38 m 47 sec
Reputation Power: 2
Quote:
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

Reply With Quote
  #5  
Old August 1st, 2011, 01:28 PM
Frank Grimes's Avatar
Frank Grimes Frank Grimes is offline
Plays with fire
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2003
Location: Outside looking in
Posts: 894 Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level)Frank Grimes User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 4 h 40 m 15 sec
Reputation Power: 95
You can name MovieClips. It works the same way.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignFlash Help > ActionScript 3 - Flash based Tetris

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap