View Full Version : falling objects problem
shrijani
January 29th, 2009, 11:33 AM
Hello,
I'm using the code in http://board.flashkit.com/board/show...hlight=falling for my catch game. At the moment the objects are falling from the sky and disappearing when they hit to my object (which is a stick) What I want to do is to catch the objects and stack them on top of each other on the stick, and make them move together with the stick to catch new objects.
Can someone help me please?
Thank you.
rrh
January 29th, 2009, 11:55 AM
Your link's bad, try again.
Also, this is easier in AS3 than AS2, since then you can just addChild anything to anything else. Which are you using?
pradvan
January 29th, 2009, 12:28 PM
Can you post a fla or some code?
shrijani
January 29th, 2009, 12:40 PM
I'm sorry, the correct link is: http://board.flashkit.com/board/showthread.php?t=786723
The code looks like this:
Actionscript on Fr 1 On main _root timeline:
//this produces random balls that fall from the sky
depth = 0;
allBalls = new Array();
function makeNewClip() {
clearInterval(ranID);
ran = (Math.random()*250)+500;
ranID = setInterval(makeNewClip, ran);
newClip = _root.attachMovie('good', 'good'+depth, depth++);
allBalls.push(newClip);
//trace(allBalls);
newClip._x = 140 + Math.random()* 430;
newClip._y = -50;
newClip.speed = (Math.random()*5)+5;
newClip.onEnterFrame = function() {
this._y += this.speed;
if (this._y>Stage.height) {
updateScore(-2);
for (i=0; i<allBalls.length; i++) {
if (this == allBalls[i]) {
allBalls.splice(i, 1);
//trace(allBalls)
}
}
this.removeMovieClip();
}
};
}
//
makeNewClip();
//
//
_root.createEmptyMovieClip('watchCollision', -2);
watchCollision.onEnterFrame = function() {
for (i=0; i<allBalls.length; i++) {
if (allBalls[i].hitTest(cocktail_mc._x, cocktail_mc._y, true)) {
//trace('we hit the cocktail');
allBalls[i].removeMovieClip();
allBalls.splice(i, 1);
updateScore(5);
}
}
};
score = 0;
function updateScore(amount) {
score += amount;
score_txt.text = 'Score is '+score;
}
updateScore(0);
//Actionscript on slider MC, my slider MC is called 'cocktail':
onClipEvent (load) {
power = 0.5;
yspeed = 0;
xspeed = 10;
friction = 0.95;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
xspeed -= power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed += power;
}
xspeed *= friction;
yspeed *= friction;
_y += yspeed;
_x += xspeed;
}
Well, I am using AS2 at the moment. My question is, how to stick the objects on top of each other, and move them together with the slider..
pradvan
January 29th, 2009, 12:46 PM
How good are you with AS? Do you understand the code in that example?
shrijani
January 29th, 2009, 12:49 PM
Yes, I do. I know that I need to make some changes under watchCollision function, attach the falling objects to cocktail_mc, make their x value equal to x of cocktail_mc. But I just couldn't figure out how to place the code in, and also make the falling objects stick to my skewer object (kind of like filling it upwards)
pradvan
January 29th, 2009, 01:14 PM
Ok, just checking. You seem to know what you're talking about. Some people just come here to have others do the work for them...
Here's some pseudo code:
watchCollision.onEnterFrame = function() {
for (i=0; i<allBalls.length; i++) {
var tempBall = allBalls[i];
if (tempBall.hitTest(cocktail_mc)) {
//trace('we hit the cocktail');
var dupedBall = cocktail_mc.duplicateMovieClip(tempBall, "ball"+cocktail_mc.getNextHighestDepth(),cocktail_mc.get NextHighestDepth())
dupedBall._x = cocktail_mc._x-tempBall._x;
dupedBall._y = cocktail_mc._y-tempBall._y;
tempBall.removeMovieClip();
allBalls.splice(i,1);
updateScore(5);
}
}
};
I think i might have screwed up the duplicateMovieClip function (hard to test without the fla) but this should give you an idea on how you can do it, and the equation to calculate the new x of the ball inside the paddle.
-PR
shrijani
January 29th, 2009, 01:26 PM
Ok, thank you for this. I'll try it in my fla and post it here, to understand better.
shrijani
January 29th, 2009, 02:02 PM
It says the file is too big, so I'm posting my code below. At the moment, I have four different fruits falling from the sky. I have included the code you have given me, but it changes the position of the slider, rather than the fruits. What I want to achieve is that the fruits will be attached to the slider, and fill it up horizontally.
stop();
_global.seconds = 10;
//this produces random balls that fall from the sky
depth = 0;
allBalls = new Array();
function makeOne() {
if(seconds>0){
clearInterval(ranOneID);
ran = (Math.random()*4000)+1000;
ranOneID = setInterval(makeOne, ran);
One = _root.attachMovie('circle', 'circle'+depth, depth++);
allBalls.push(One);
//trace(allBalls);
One._x = Math.random()*550;
One._y = -50;
One.speed = (Math.random()*10)+5;}
One.onEnterFrame = function() {
this._y += this.speed;
if (this._y>Stage.height+100) {
//updateScore(-5);
for (i=0; i<allBalls.length; i++) {
if (this == allBalls[i]) {
allBalls.splice(i, 1);
}
}
this.removeMovieClip();
}
}
}
function makeTwo() {
if(seconds>0){
clearInterval(ranTwoID);
ranTwoID = setInterval(makeTwo, ran);
Two = _root.attachMovie('square', 'square'+depth, depth++);
allBalls.push(Two);
//trace(allBalls);
Two._x = Math.random()*550;
Two._y = -100;
Two.speed = (Math.random()*9)+5;}
//spin = (Math.random() * 6) - 1;
//Two._rotation=random(360)+1;
Two.onEnterFrame = function() {
this._y += this.speed;
if (this._y>Stage.height+100) {
//updateScore(-5);
for (i=0; i<allBalls.length; i++) {
if (this == allBalls[i]) {
allBalls.splice(i, 1);
}
}
this.removeMovieClip();
}
};
}
function makeThree() {
if(seconds>0){
clearInterval(ranThreeID);
ranThreeID = setInterval(makeThree, ran);
Three = _root.attachMovie('three', 'three'+depth, depth++);
allBalls.push(Three);
//trace(allBalls);
Three._x = Math.random()*550;
Three._y = -100;
Three.speed = (Math.random()*8)+5;}
Three.onEnterFrame = function() {
this._y += this.speed;
if (this._y>Stage.height+150) {
//updateScore(-5);
for (i=0; i<allBalls.length; i++) {
if (this == allBalls[i]) {
allBalls.splice(i, 1);
}
}
this.removeMovieClip();
}
};
}
function makeFour() {
if(seconds>0){
clearInterval(ranFourID);
ranFourID = setInterval(makeFour, ran);
Four = _root.attachMovie('four', 'four'+depth, depth++);
allBalls.push(four);
//trace(allBalls);
Four._x = Math.random()*550;
Four._y = -100;
Four.speed = (Math.random()*7)+5;}
Four.onEnterFrame = function() {
this._y += this.speed;
if (this._y>Stage.height+100) {
//updateScore(-5);
for (i=0; i<allBalls.length; i++) {
if (this == allBalls[i]) {
allBalls.splice(i, 1);
}
}
this.removeMovieClip();
}
};
}
makeOne();
makeTwo();
makeThree();
makeFour();
_root.attachMovie('slider', 'slider_mc', -1);
slider_mc._y = Stage.height-20;
maxSpeed=15;
xSpeed=0;
//KEYBOARD PROPERTIES
watchKeyboard = new Object();
watchKeyboard.onKeyDown = function() {
clearInterval(easeID);
if (Key.getCode() == Key.LEFT) {
//slider_mc._x -= 20;
if(xSpeed>-maxSpeed){
xSpeed--;
}
}
if (Key.getCode() == Key.RIGHT) {
if(xSpeed<maxSpeed){
xSpeed++;
}
//slider_mc._x += 20;
}
};
watchKeyboard.onKeyUp=function(){
clearInterval(easeID);
easeID=setInterval(ease,5);
}
slider_mc.onEnterFrame = function() {
if(score >= 50) {
this.removeListener(watchKeyboard);
}
Key.addListener(watchKeyboard);
this._x+=xSpeed;
if ( this._x >=560){this._x=560;}
if ( this._x <= 10){this._x=10;}
updateAfterEvent();
}
function ease(){
if(xSpeed>0){
xSpeed--;
}
if(xSpeed<0){
xSpeed++;
}
if(xSpeed==0){
clearInterval(easeID);
}
}
_root.createEmptyMovieClip('watchCollision', -2);
watchCollision.onEnterFrame = function() {
for (i=0; i<allBalls.length; i++) {
var tempBall = allBalls[i];
if (tempBall.hitTest(slider_mc)) {
//trace('we hit the cocktail');
var dupedBall = slider_mc.duplicateMovieClip(tempBall, "ball"+slider_mc.getNextHighestDepth(),slider_mc.getNext HighestDepth())
dupedBall._x = slider_mc._x-tempBall._x;
dupedBall._y = slider_mc._y-tempBall._y;
tempBall.removeMovieClip();
allBalls.splice(i,1);
updateScore(10);
}
}
};
score = 0;
function updateScore(amount) {
score += amount;
score_txt.text = 'Score is '+score;
}
updateScore(0);
shrijani
January 30th, 2009, 06:07 AM
anyone??
pradvan
January 30th, 2009, 01:29 PM
Alrighty, first, post code in code tags. That way its nicely formatted and easy to read.
Second, you'll need to post your fla if you want me to look in to this further. Either zip it to reduce size, or get rid of some heavy art and zip it. You can also upload it somewhere if its too big for the forums.
Cheers
shrijani
February 2nd, 2009, 06:41 AM
Hello again,
Here is my fla:
http://www.silaisik.com/catch4.fla
Thank you very much.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.