View Full Version : Game like; 625 Sandwich Stacker
Amymation
May 23rd, 2010, 10:07 AM
Hey,
I have a college project which is pretty huge and I decided to code a flash game. I have an idea of what I want to do, it's something like; http://www.sugar-free-games.com/playgame.php?game=971
I know how to structure it all and some coding but it's the random code for food to be dropped (good & bad) which confuses the hell out of me.
Just wondering could someone give me links or go through with what I got to do thoroughly with code (commented out so I learn). Or even send me a .fla with the basics of what I need (Food code etc..).
The thing is that I really do want to learn I just need code and it to be commented out.
- Thanks.
Amy.
(ActionScripting 2 is what i use).
therobot
May 23rd, 2010, 11:04 AM
This isn't terribly difficult :)
You'll want to familiarize yourself with generating random numbers in flash. Check out this tuturial: http://www.kirupa.com/developer/actionscript/tricks/random.htm
In a nutshell, calling Math.random() in flash will generate a random decimal between 0 and 1. With a little bit of math, you can use this to generate any random number you will need for your game.
Here's a simple example that will generate a random number from 0 to the Stage's width. You can use this to get the starting position of your sandwich fixins before they fall down the screen:
fixin._x = Math.random() * Stage.width;
Ok, the next thing to do is figure out which fixin to drop. Going with that sandwich example, there are bad drops and good drops. Let's say you want bad things to drop 20% of the time - this is easy:
if ( Math.random() < 0.20 )
{
trace( "drop a bad thing!" );
}
else
{
trace( "drop a good thing!" );
}
Since you're using as2, this is how I would probably attach the items on stage. Have two arrays that contain strings which specify the linkage name of the movieclips. Like so:
var goodItems:Array = ["onion", "cheese", "bread", "tomato", "lettuce", "mayo", "pickle"];
var badItems:Array = ["moldyCheese", "moldyBread", "motorOil", "ratPoison"];
// now, whenever you want to drop a new item, we'll create a pointer Array that can point to either our bad items or our good items
var itemPool:Array;
if ( Math.random() * 0.20 )
{
itemPool = badItems;
}
else
{
itemPool = goodItems;
}
// now we know which array to use...let's get a random item in that array.
var arrayIndexToUse:Number = Math.floor(Math.random() * itemPool.length);
var itemToUse:String = itemPool[arrayIndexToUse];
trace( "we are going to spawn in " + itemToUse );
// now attach the movieclip - get the next highest unused depth first so we don't accidentally delete anything
var depth:Number = _root.getNextHighestDepth();
var item:MovieClip = _root.attachMovie(itemToUse, "item_" + depth, depth);
// now position our item randomly across the stage.
item._x = Math.random() * Stage.width;
item._y = 0 - item._height; // this should put the item at the top of the screen.
Amymation
May 23rd, 2010, 11:26 AM
This isn't terribly difficult :)
You'll want to familiarize yourself with generating random numbers in flash. Check out this tuturial: http://www.kirupa.com/developer/actionscript/tricks/random.htm
In a nutshell, calling Math.random() in flash will generate a random decimal between 0 and 1. With a little bit of math, you can use this to generate any random number you will need for your game.
Here's a simple example that will generate a random number from 0 to the Stage's width. You can use this to get the starting position of your sandwich fixins before they fall down the screen:
fixin._x = Math.random() * Stage.width;
Ok, the next thing to do is figure out which fixin to drop. Going with that sandwich example, there are bad drops and good drops. Let's say you want bad things to drop 20% of the time - this is easy:
if ( Math.random() < 0.20 )
{
trace( "drop a bad thing!" );
}
else
{
trace( "drop a good thing!" );
}
Since you're using as2, this is how I would probably attach the items on stage. Have two arrays that contain strings which specify the linkage name of the movieclips. Like so:
var goodItems:Array = ["onion", "cheese", "bread", "tomato", "lettuce", "mayo", "pickle"];
var badItems:Array = ["moldyCheese", "moldyBread", "motorOil", "ratPoison"];
// now, whenever you want to drop a new item, we'll create a pointer Array that can point to either our bad items or our good items
var itemPool:Array;
if ( Math.random() * 0.20 )
{
itemPool = badItems;
}
else
{
itemPool = goodItems;
}
// now we know which array to use...let's get a random item in that array.
var arrayIndexToUse:Number = Math.floor(Math.random() * itemPool.length);
var itemToUse:String = itemPool[arrayIndexToUse];
trace( "we are going to spawn in " + itemToUse );
// now attach the movieclip - get the next highest unused depth first so we don't accidentally delete anything
var depth:Number = _root.getNextHighestDepth();
var item:MovieClip = _root.attachMovie(itemToUse, "item_" + depth, depth);
// now position our item randomly across the stage.
item._x = Math.random() * Stage.width;
item._y = 0 - item._height; // this should put the item at the top of the screen.
Thanks! This is great help.
I've implemented everything you gave me, and i actually understand it haha. A few questions though if you don't mind?
1. How would I get the objects to move down now? For some reason one item just spawns at the top.
2. How would I make more than one fall down as the levels progress?
3. How could I get the ingrediants to stack ontop of the first piece of bread once it's hit the character?
I've supplied you with my fla I have so far. The graphics are very lame and don't make sense because I just want the coding sorted before I mess with that kind of stuff.
Click for FLA (http://www.amymations.com/downloads/wiigame-2.fla)
I may sound like a complete n00b but funily I'm not haha, I just haven't ventured into this type of game before.
Sorry for being a pain in the butt. ><
- Credit will also be given in my site/game which will be presented infront of alot of important people.
therobot
May 23rd, 2010, 01:27 PM
I'm not doing yr homework for you :P
1. How would I get the objects to move down now? For some reason one item just spawns at the top.
You'll want to investigate onEnterFrame. Essentially it's a function that is called every frame. You can use it to update a MovieClip's position every frame.
2. How would I make more than one fall down as the levels progress?
You'll want to look at setInterval. It'll let you call a function every XX milliseconds. The point being you can specify the time delay between the next function call.
3. How could I get the ingrediants to stack ontop of the first piece of bread once it's hit the character?
sounds fun. i would remove the falling movieclip from the stage and attach a new movieclip to your sandwich.
Amymation
May 23rd, 2010, 01:53 PM
I'm not doing yr homework for you :P
You'll want to investigate onEnterFrame. Essentially it's a function that is called every frame. You can use it to update a MovieClip's position every frame.
You'll want to look at setInterval. It'll let you call a function every XX milliseconds. The point being you can specify the time delay between the next function call.
sounds fun. i would remove the falling movieclip from the stage and attach a new movieclip to your sandwich.
Ah right, Well I've done onEnterFrame before, but never ventured to arrays and mixing the two, didn't know if it'd work. But I'm guessing it does haha. So I'd need coding structured like onEnterFrame > if the object is on stage > fall down y axis. Something like that, I think I got it.
- Cheers!
Edit:
Okay well I can't figure it out. I tried putting
onEnterFrame = fucntion () {
item._y ++;
}
No errors appears it just doesn't work ...
Amymation
May 23rd, 2010, 03:25 PM
Been trying to figure it out for hours ><. I know what to do, it's just how to do it. I don't use onEnterFrame functions, gah it's difficult.
genericguy
May 25th, 2010, 12:34 AM
you almost had it. it's ... item._y +=5; 5 being any number you want.
http://www.kirupa.com/developer/actionscript/gravity.htm
thats a simple tutorial on falling objects.
genericguy
May 26th, 2010, 07:11 PM
Hey therobot, I've been playing around with your code that randomly selects a moviclip to attach. I'm trying to figure out how to give each movieclip ("onion", "cheese", etc.) a unique point system. For example... if the user catches the onion its worth 10 points, if he catches the cheese its worth 20 points. How do you figure out what movieclip is being attached?
This doesn't work but it will let you know what I'm trying to do...
if (itemToUse == cheese){
do something
}
thanks.
therobot
May 26th, 2010, 08:53 PM
it should be
if (itemToUse == "cheese") trace( "it's cheese"!)
if you were feeling adventurous, you could make an Object for each...object:
var cheese:Object = {linkageName:"cheese", points:3};
var onion:Object = {linkageName:"onion", points:6};
var goodItems:Array = [cheese, onion];
var dirt:Object = {linkageName:"dirt", points:-1};
var badItems:Array = [dirt];
See what i just did? badItems[0] would now refer to the dirt object I created, so I could say badItems[0].points to get the points without needing to know specifically what the object is.
So you'd have to modify the code a little bit, but you'll see it makes things nice & neat:
var itemToUse:Object= itemPool[arrayIndexToUse];
trace( "we are going to spawn in " + itemToUse.linkageName );
trace( itemToUse.linkageName + " is worth " + itemToUse.points );
genericguy
May 27th, 2010, 05:16 PM
thanks!
Amymation
May 29th, 2010, 03:06 PM
hey guys,
Sorry but you've completely lost me. Could you go through with me how to put a score system in step by step?
I'm not used to arrays and this project has to be handed in soon.
Thanks,
-Amy
Amymation
June 13th, 2010, 10:52 AM
[SOLVED]
Fixed this ... took me 5 days to realise there's an extra item in the goodItems array [tomato] so whenever the game was trying to spawn one it couldn't find it.
Thanks for the help guys.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.