dcsimg
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [F8] another hittest problem....

Hybrid View

  1. #1
    Senior Member
    Join Date
    Oct 2006
    Posts
    221

    [F8] another hittest problem....

    normally this works or at least i have had other situations where it did.
    i have attached a movie clip to the root level with another movieclip. when the attached movie clip hits another movieclip on stage, the movieclip should unload, but the hitTest never works.
    my code:
    this is in the attacher movieclip:
    Code:
    onClipEvent (load) {
    	depthcounter = 1;
    }
    on (release) {
    	_root.attachMovie("bulletsbl", "bulletbl"+depthcounter, depthcounter);
    	_root["bulletbl"+depthcounter]._x = this._x;
    	_root["bulletbl"+depthcounter]._y = this._y;
    	depthcounter++;
    }
    and in the movieclip being hit, or supposed to be hit:
    Code:
    onClipEvent (enterFrame) {
    	if (_root["bulletbl"+depthcounter].hitTest(this)) {
    		this.unloadMovie();
    		trace("hit")
    	}
    }
    why won't this work, i don't understand it

  2. #2
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    Hey squidgie,

    Here's why: Look at the last thing that your onRelease handler does. It increments the depthcounter variable. This means that now depthcounter is one value more than the one in the name of the object you just created.

    So when you run the hitTest, there is no such object out there with that name.

    Try taking away the line where you increment the depthcounter variable, and see if it works.

  3. #3
    Senior Member
    Join Date
    Oct 2006
    Posts
    221
    i tried that and it doesn't work. all it did was disable multiple shots lol

  4. #4
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    Hey again, squidgie,

    How are you creating your character movie clip? At authoring time? If so, you can't remove it without swapping it's depth to a positive number. Still, you should be getting the trace action.

    To tell you the truth, you should first abandon object code (and the "on" clip events) in favor of frame code. Put all your code on frame 1 of the main timeline in an actions layer. There is no downside to this; it's just so much easier to work with. The only requirement to it is that you have to give all your objects instance names.

    By doing this, you can now set and remove onEnterFrame functions, which you cannot do using object code. There are many other advantages as well, the main one being that your code is all in one place, and not scattered around the file.

    I'm attaching a file that will help you. I made a ball and a bullet movie clip. The bullet gets attached to a random y location at the right side of the screen. If a bullet hits the ball, it gets deleted and the bullet gets deleted.
    Attached Files Attached Files

  5. #5
    Senior Member
    Join Date
    Oct 2006
    Posts
    221
    ok cool, thank you lol. one question:theInterval = setInterval(this, "createBullet", 500);. what exactly is this?

    also, would i have to include something like _root.mc.onRelease{function}
    or something like that to make the bullets attach when the mc is clicked on?
    Last edited by squidgie90; 04-15-2007 at 11:40 AM.

  6. #6
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    I found that introducing a new bullet using onEnterFrame gave me too many too fast. setInterval is a function that lets you call another function repeatedly at whatever rate you want, independent of the framerate. But I'll take it apart for you:

    theInterval is just a variable name I made up. It identifies the setInterval, and if you trace it out, you'll find it's just a number. Using it later with clearInterval turns the interval off again.

    And here are the parameters:

    this -- I've found that when you use setInterval to call any function that uses the keyword "this" inside of it, you have to use this longer form of setInterval:
    setInterval(scope, function name in quotes, milliseconds);

    function name -- the name of the function to call on an interval. In this case, createBullet.

    500 -- the function will be called every 500 milliseconds, or every half second.

    I realize that this may seem incredibly technical to you, it did to me, too. But I've done a ton of tinkering with it, and it gets easier and more familiar and soon you find you can make it do exactly what you want.

    The ordinary form of setInterval is easier:
    intervalName (you get to make this up) = setInterval(function, milliseconds);

    In this form, the function name should NOT be in quotes. But if the function being called uses the keyword "this" anywhere inside it, use the longer form instead.

    And anyway, you could get rid of the interval, and make a shooter game out of it instead, which is what I suspect you really want to do anyway. Just make some other event introduce a new bullet.

  7. #7
    Senior Member
    Join Date
    Oct 2006
    Posts
    221
    ok, i thought it was something like that, just wanted to make sure, thank you again.

  8. #8
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    I realize this is bare-bones, and has a lot of room for improvement to make it interesting. But here is the possible beginning of a left-right shooter game (attached). Use left and right arrow keys to move, spacebar to shoot. Hope that helps a bit.
    Attached Files Attached Files
    Last edited by Mazoonist; 04-15-2007 at 01:09 PM.

  9. #9
    Senior Member
    Join Date
    Oct 2006
    Posts
    221
    heh. the game i am making is going to have a rotating circular enemy that bounces off the sides of the screen. the enemy is lined with different colored targets on the edges. there are 4 different colored guns on the corners of the screen, the bullets must match the color of the targets to hit them, to gain ammo for each gun, you must click and drag that color ammo icon that appears randomly

  10. #10
    Senior Member
    Join Date
    Oct 2006
    Posts
    221
    ok, this deals with the topic at hand, sorta lol.
    um what if there are multiple targets like 20 on the stage. how would you code the hitTest to hit all of the 20 targets?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center