?
Solved

Replace one movieclip with another on hitTest??

Posted on 2005-03-26
37
Medium Priority
?
592 Views
Last Modified: 2008-01-09
Hello - please can someone help!

I'm using Flash MX and I am producing a very simple game. ( Not quite simple enough though it would seem!)

My problem is this - I have a bottle being released which is meant to fall into a bin. If it misses the hole and hits the side of the bin, or the ground, it should smash. So I want to replace the movie clip 'bottle' with the movie clip 'smash'. How do i achieve this seemingly easy task? I have played around with loadmovie/unloadmovie but nothing really works.

Secondly, if the bottle goes into the hole, I want tthe score to increase by 10. So i set up a hitTest between tthe bottle mc and the hole mc to increment the dynamic text box by ten, using an on clipevent ( enterframe). It increases the score ok, but it won' stop at 10 - it just keeps on going!

Any ideas?
0
Comment
Question by:isotherm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 20
  • 16
37 Comments
 
LVL 4

Expert Comment

by:armeen
ID: 13637796
errm i am pretty new to flash so there might well be better way, but what i would do is have the smashed bottle as a different movie clicp and if your bottle misses the bin and hits the floor hide the bottle ( ._visible = false ) and show the smashed bottle at the same ._x and._y

No 2 - I am not sure but the help has this:

Description


Event handler; invoked continually at the frame rate of the movie. The actions associated with the enterFrame clip event are processed before any frame actions that are attached to the affected frames.


so i am not surprised it goes up and up - maybe at the end of your function call "stop()" ??
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13638167
something like this in a loop for the bottle:

if (this.hitTest(_root.bin)) {
      dep2 = _level1.clip.getNextHighestDepth();
      _level1.clip.attachMovie("smash", "smash", dep2);
      _root.smash._x = _x;
      _root.smash._y = _y;
      removeMovieClip(this);
}

and same for the inside of the bin, build a hit area for the inside of the bin, name its instance hitt,a dn do this
if (this.hitTest(_root.bin.hitt)) {
_root.score +=10;
      removeMovieClip(this);
}

Billystyx
0
 

Author Comment

by:isotherm
ID: 13638610
Thanks for that guys - armeen - I'm not sure how to display the 'smash' mc at the same x/y...I did consider hiding the bottle when it 'hit', though I was attempting to have the two movie clips inside another movie clip, but it all got far too complicated!

Billystyx - I tried your actionscript as you suggested, but unfortunately it  doesn't seem to work. Maybe something to do with 'nexthighestdepth'....do I need to have the clips in a certain order, on the stage? Or does it call from the library?
I can post the .fla if it's allowed and would help?

Confused! Thanks anyway....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Billystyx
ID: 13638737
Yes, posting the fla is a good idea

Billystyx
0
 

Author Comment

by:isotherm
ID: 13638917
Hi Billystyx,

ok the .fla is at:

http://www.holmes.7host.com/smashemup_1.fla

Thanks for your help.
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13639258
There is possibly quite a lot of work to make your swf do what you want it to here - To do a hitTest between 2 objects they need to be both mcs for a start, so the bins would need to be converted from  grouped elements to each one being a single mc.
You then have the choice of using armeen's option (and based on the fact that the mcs are not dynamcally loaded and therefore cannot be dynamically removed, using the option of making one invisible and the other visible based on a hitTest seems the least work.
If you do decide to use the attchMovie and removeMovieClip methods to manipulate the reactions between the bin and the bottle I think it would provde a smoother and less byte intensive swf for the final product, but otherwise, start by converting the bins to mcs, and placing an invisible mc inside each and naming it as Hitt or something similiar, so you can test for a hitTest between the bottle, the bin and also the bins insides for different scenarios.
Hope this helps a bit

Billystyx
0
 

Author Comment

by:isotherm
ID: 13639932
Hi Billystyx,

thanks for having a look at it - there is actually a mc called' hitt' on the green bin - I was just trying to get that working first.
I will attempt the way that armeen suggested; I understand how to make one clip invisible, but not too sure about how you substitute it with another clip at the same loc.

I think also the fact that the bottle mc is inside two others is causing 'level' probs - I thought that '_root.mymc' would always fiind the target, but it seems not. Oh well.

Thanks for your patience!

cheers
Jim
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13640394
I would say give it a try and post what you have and we can work it through.
Bottlesmash should probably be brken into a falling clip and a smashing clip. If you don't want this, put a
stop();
on the fall, and then test with the greenhitt. Like I did above,
if(greenhitt.hitTest(_root.wholerig.bottlesmash){
root.wholerig.bottlesmash.gotoAndPlay("smashing");//where smashing the frame the smashing starts at

}

Billystyx
no need to make invisible since its all part of the same clip in your example


0
 

Author Comment

by:isotherm
ID: 13641025
Hi Billystyx,

I have spent hours trying to get this to work to no avail - nothing seems to work :-(
I made sure the instance names were correct...frame names etc..but it just doesn't want to know.

Have uploaded it to the same place again - if you have time to take a look i'd be grateful!

I also tried toget the 'attachmovie' to work - but nothing......I'm clearly missing something here!

cheers
Jim
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13642239
I will take another look:)
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13643046
I have a fla here, with the 1st scene and the first bottle and the left side of the bin operating the way it should. Take a look and you should be able to use the method here (as described above) for all three bins/scenes.
http://members.lycos.co.uk/billystyx/download.htm
Its the one called controlerv2 in the top left corner, and there are several flas but yours is named as your fla, though v3

Billystyx
0
 

Author Comment

by:isotherm
ID: 13643456
Fantastic - I will go through it tonight and learn :-)

Thanks Billystyx mucho appreciated.

cheers
Jim
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13645422
no worries:)
0
 

Author Comment

by:isotherm
ID: 13701479
Hi Billystyx,
still having big probs - I've been trying to hide the 'bottlesmash' mc that was dynamically attached to the 'bottle1' clip when it too gets hidden but I just cannot find the correct level/path/whatever. Have tried every variation I can think of:

_root.bottle1.bottlesmash
_parent._parent.bottlesmash
etc etc etc

but nothing works. Also, if the bottle hits the hole and the score increments to '10', I need the movie to go to the next scene - if it doesn't hit the hole and the score remains at 0, then I need the bottle to re-attach to the 'rig'  so the player can try again.

I'm seriously running out of time on this project, so if you fancy earning a few dollars to finish it let me know!!

cheers and thnx,

Jim
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13701959
sorry jim, it is against site rules to do for money (for us experts anyway),

_root.bottlesmash is the one you are looking for.

if you look into the whole_rig copy clip (at the bottom of the library), you will find the correct clip (sorry I didn't rename and delete the mcs no longer needed).

bottle1 in the library is the bottle, and bottlesmash is the bottlesmash clip.
If you put in 30 or so frames on the bottlesmash clip and take out the stop();
and on the last frame of it create a keyframe with

_root.attachMovie("bottle1","bottle1",21);
_root.bottle1._x=_root.rig._x;
_root.bottle1._y=_root.rig._y;
_root.rig.dir=1;
removeMovieClip(this);

it should remove the smashed bottle after2 or 3 seconds and replace the rig with another bottle.
Wherever the score increment to 10 (where the code for that is), put
_root.rig.removeMovieClip();
_root.bottlesmash.removeMovieClip();
_root.gotoAndPlay("Scene 2");

Billystyx
0
 

Author Comment

by:isotherm
ID: 13703224
Sorry - I didn't realise it would be against the rules, but armed with this new info I will have another go!
Thanks Billy - you are a star!

Jim
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13704405
no worries -good luck with it:)
0
 

Author Comment

by:isotherm
ID: 13705986
Hi Billystyx,

right - the re-attach bottle to rig is working great - but the code that hides the bottlesmash and takes the game to the next scene unfortunately doesnt :(

onClipEvent (enterFrame) {
    if (this.hitTest(_root.bottle1)) {
        _root.score += 10;
        setProperty("_root.bottle1", _visible, "0");
        setProperty("_root.bottlesmash", _visible, "0");
        _root.bottlesmash.removeMovieClip();
        _root.gotoAndPlay("clear_level1");
    }
}

Any ideas?
Thnx! Jim
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13706220
is clear_level1 the name of scene 2?

Billystyx
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13706247
if so,
_root.gotoAndPlay("clear_level1",1);

should work...

Billystyx
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13706256
if clear_level1 is the name of the frame label for the 1st frame of the second scene, then
_root.gotoAndPlay("Scene 2", "clear_level1");

billystyx
0
 

Author Comment

by:isotherm
ID: 13706415
Hi Billy:

onClipEvent (enterFrame) {
      if (this.hitTest(_root.bottle1)) {
            _root.score += 10;
            setProperty("_root.bottle1", _visible, "0");
            setProperty("_root.bottlesmash", _visible, "0");
            _root.gotoAndPlay("Scene 2", "clear_level1");
            _root.bottlesmash.removeMovieClip();
      }
}

..still does nothing...I don't understand why. I also tried:  ("_root.Scene 2"......) to no avail.

Also this: setProperty("_root.bottlesmash", _visible, "0");
element is not working, but:

setProperty("_root.bottle1", _visible, "0");

does!

Confused....I have uploaded it to the same place if you need to see it in relation to everything else.

cheers,
Jim



0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13706492
I don't have flash in front of me at the moment - but if still no luck tonight I will pick up.
But, check the instance name of bottlesmash - it is called bottlesmash in the library, but if you look in the bottle1 clip (I think), this is where the bottlesmash gets attached to the stage. in the attachMovie line, check what the name in the 2nd parameter is, and also where it is being attached to (like _root, or _root.bottle1), this will give you the correct instance name.

For Q1, try leaving off the _root next to the gotoAndPlay, and also try leaving off the second parameter. For some reason I always have trouble with scenes, and always get there after a couple of attempts and then never remember what I did last time!

Billystyx
0
 

Author Comment

by:isotherm
ID: 13707082
Nope - no luck. I've tried every variation I can think of! Also scene 2 itself is doing strange things. If you play scene 1 first, then scene 2 it doesn't start, but if you open the file, then go straight to play scene 2, it works!
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13707105
I will take a look - but I won't be able to for another 4 hours, sorry

Billystyx
0
 

Author Comment

by:isotherm
ID: 13707250
OK - thnx!
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13711882
try this
_root.gotoAndPlay(17);
it works... (17 is the next frame past the last frame on scene1)

make sure you paste it into the hitt instance on scene1...

Billystyx
0
 

Author Comment

by:isotherm
ID: 13712284
How/why does that work? It's great that it does though!
Thanks Billy. When I get to the next level in the game ( i.e., level 2) I assume that I can just alter the x y co-ordinates for the attach whole_rig script to get it on the higher bar?

Thanks again for your help,

Jim
0
 

Author Comment

by:isotherm
ID: 13712322
I also have to try and resolve the problem of controlling the score going up only by 10 - not as currently sometimes 10 - sometimes 20 --30!

onClipEvent (enterFrame) {
      if (this.hitTest(_root.bottle1)) {
            _root.score += 10;

I tried putting a 'stop' after this, but it had no effect?

Jim

0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13712323
yes, that seems the way to go - it works because if you have 10 frames on scene1 _root timeline and you have a scene2, then scene2 will start at the 11th frame - it is as simple as that. scene2 is just another way of referencing it - which I always have a problem with (soe things just don't sink in I guess)

Billystyx
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13712354
you could try
onClipEvent(load){
hitBottle="false";
}
onClipEvent (enterFrame) {
     if (this.hitTest(_root.bottle1) && hitBottle="false") {
        hitBottle="true";
}
}

and then where the bottle gets 'refreshed' put
hitt.hitBottle="false";
(or whatever path this is on to ref it)

Billystyx
0
 

Author Comment

by:isotherm
ID: 13712429
OK - I'll give it a shot! But how does this stop the counter at 10...

Sorry - my brain is turning to mush at this time of night!

cheers
Jim
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13714229
because 'true' is not incrementing - it either is or isn't. so when the bottle disappears that is when the score increments (and happens only once). whereas a hittest canhappenseveral times ina second.
Billystyx
0
 
LVL 18

Accepted Solution

by:
Billystyx earned 500 total points
ID: 13770389
How did you go on this one, isotherm. If all is good can you please close?

Thanks, and good luck with it:)

Billystyx
0
 

Author Comment

by:isotherm
ID: 13804479
Hi Billystyx,
sorry for delay in replying - just moved house....chaos!

Yes - all sorted thanks - looked and worked well thanks to you.

Much appreciated

cheers
Isotherm
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13804520
no worries glad to help:)
Billystyx
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question