Log in

View Full Version : duplicate movieclips hittest problem, can anyone help?



terry23
01-15-2009, 03:44 PM
Hi guys, I am designing a flash game and I am having a bit of trouble with the Actionscripting, I wondered if you might be able to help me? I will try and explain how the game should function and I attach a JPG that shows the rough layout as well, hopefully from that you may be kind enough to point me in the right direction.

How it should function

The idea is you have a glass that you control, left and right keyboard arrows control left and right movements and it only moves on the _x axis and the _y axis always stays at it's current value, this works fine and I have managed to add actionscript to the glass movieclip to do this, the movement also has gravity and friction which means the glass slides on a little even when you move to go the other way.

Using the Glass you need to collect falling object movieclips, there are two types, good and bad. When you catch the good it adds to your score and when you catch the bad it deducts from your score.

The falling objects need to be random and I have successfully managed to get 1 movieclip to fall by using actionscript code and also I have managed to set the _x position as random each time , the actionscript to do this has been applied to the falling object.

When the falling object hits the ground (which is also a movieclip) I want it to smash and the broken bits to stay at the bottom of the screen where it has smashed. This needs to apply to both the good and bad MCs.

There are three other hittests I would like, one is on the left edge of the rim of the glass and another on the right edge (this means that if the falling object does not fall in the middle of the glass and on the edge it will smash) the other is a hittest inside the glass that detects when the falling object has successfully landing inside, this also adds to the score.

To the left of the screen there is also a timer which acts a bit like a preloader bar, when it is complete 'time up' it will action a goto command.

My problem

I am new to programming like this in flash and so i am finding it quite difficult to understand some things, despite looking at several tutorials and forums online I just can't seem to get the multiple objects falling. I basically want to create duplicate movieclips from the good and bad ones, be able to set their 'fall speed' and 'frequency of objects to fall at any one time' and then ensure that they each keep the same action properties to trigger the hittests and increase the score, each needs to smash still and they all need to work independently of each other. I did manage to get multiple objects to enter the stage but they all fell together in one line along _y. It needs to be a random fall.

As a guide the objects included are:

MC name: glass instance name: glass1 (MC that user controls)
MC name: good instance name: good1 (Falling object that adds to score)
MC name: bad instance name: bad1 (Falling object that subtracts from score)
MC name: ground instance name: ground1 (The ground where good/bad MCs smash)
MC name: rimL instance name: rimL1 (Left edge of glass, good/bad smash when in contact - hittest)
MC name: rimR instance name: rimR1 (Right edge of glass, good/bad smash when in contact - hittest)
MC name: caught instance name: caught1 (in glass and it hittests when good or bad falls in glass and then alters the score accordingly)

I think that's all of them, but I may have missed something.

I hope you can help a bit with this, as I mentioned everything online seems to be confusing me and just needs explaining a bit clearer. Thanks in advance for any help you might be able to give me.

Terry

punstc
01-16-2009, 07:20 PM
alright I hope i can help a little bit with this. I dont have all the answers for every question but I believe I should be able to get you going in the right direction with your balls falling correctly.

I'm not sure what version of actionscript your using but this will be AS3

First off you need to create movie clips of your balls
Starting on frame to you want to give them an animation of the ball breaking
then give them linkage for actionscript so under class name for my example you'd give them the name RedBall and BlueBall

then establish them as variables on the top




var rBall:RedBall;
var bBall:BlueBall;




Ok so here are some function that will create the balls and give them their properties.




function createR() {
for (var i=0; i<(Math.random()*2); i++) { //generates a random amount of red balls
rBall = new RedBall();
rBall.x = Math.random()*850; //applies a random position for each ball horizontally
rBall.y = Math.random()*340; //applies a random position for each ball vertically
rBall.speed = Math.floor(Math.random() * 5+1); //makes a dynamic speed property so how fast they move
rBall.addEventListener(Event.ENTER_FRAME, updateBall); //adds the eventlistener that will make them move
rBall.cacheAsBitmap = true; //saves some memory
addChild(rBall); //adds each ball to the stage
}
}

function createB() {
for (var i=0; i<(Math.random()*2); i++) {
bBall = new BlueBall();
bBall.x = Math.random()*850;
bBall.y = Math.random()*340;
bBall.speed = Math.floor(Math.random() * 5+1);
bBall.addEventListener(Event.ENTER_FRAME, updateBall);
bBall.cacheAsBitmap = true;
addChild(bBall);
}
}


Now that you have balls on the stage you need the function updateBall which is an Enter Frame event to make them move




function updateBall(e:Event) {
var cBall:MovieClip = e.currentTarget as MovieClip; //creates a variable for every fish so you can reference them as one

cBall.y +=cBall.speed; //this takes the speed from the dynamic property we added in the ball functions and everyframe adds it to the y position

if (cBall.y > 600) {
cBall.goToAndPlay(2); //tells the movieclip to play its break animation
cBall.removeEventListener(Event.ENTER_FRAME, updateBall); //removes the enterframe so the ball will stop moving
}

}



there might be one or two errors in this as i'm writing it on the fly but it should get you moving in the right direction

as for the hitTestObject the problem it checks if it hit your objects bounding box "the movie clip" and not the actual ball inside. So really it could hit the corner of the movie clip box and still think its hitting the ball since a ball doesn't fit perfectly inside the box. theres always space.

I believe what you'll need to do is use bitmap data for your objects and hit test against that specially since you want to check certain areas of your cup like the rim compared to the middle. I'm not to familiar with the whole topic but i'm sure there are people on here that will be able to help you out. But look up some information regarding hit testing on bitmapdata and you should be in the right direction


hope the helps, best of luck