A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: My horsies need help

  1. #1
    Senior Member
    Join Date
    Mar 2002
    Posts
    183
    I am making a little horserace game. This is first thing I have really done on my own, scripting-wise, even if it really isn't that much. The game is you click a button, horses start going across the screen randomly, when one gets there, it goes to his respective frame with winner on it. Here is my script.
    if (_root.horse1_x>500){
    gotoAndplay(4);
    }
    if (_root.horse2_x>500){
    gotoandplay(5);
    }
    _x1=_root.horse1._x
    _x2=_root.horse1._x
    vel1=Math.random()*20
    vel2=Math.random()*20
    _root.horse1._x+=(vel1^2)
    _root.horse2._x+=(vel2^2)

    The horses just keep going, ignoring the "if" statement. Before I was getting an error saying I had a script that was causet the player to run slowly, but I guess I got rid of that somehow. But, all I want is the movie to go to a page when one of the horses goes past that certain point. But those damn horses just keep going. What's my problem??? Please help me, I have been thinking for what seems like forever.

  2. #2
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    if (_root.horse1_x>500){
    gotoAndplay(4);
    }
    if (_root.horse2_x>500){
    gotoandplay(5);
    }



    In this piece of code, shouldn't horse1_x read horse1._x and horse2_x read horse2._x

  3. #3
    Senior Member
    Join Date
    Mar 2002
    Posts
    183
    actually, I think it should. I will try that in a few mintues. Thanks a lot, I didn't even catch that.

  4. #4
    Senior Member
    Join Date
    Mar 2002
    Posts
    183
    Damn, it didn't work. Now, the horses just kind of "ignore" the if statement and go on forever. I am not getting that error anymore, but the horses just go and go and go and go.

    I would still appreicate some help.

    Thanks

  5. #5
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    OK.

    Are you using an instance name, or a movieclip name?

    You need to use the name of the instance, not the movieclip. Instance names are blank by default.


  6. #6
    Senior Member
    Join Date
    Mar 2002
    Posts
    183
    yeah, actually, i have instance names of horse1 and horse 2
    still can't figure out why this damn thing isnt working

  7. #7
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    Now I'm getting frustrated.

    Where did you put that code? Is it in a frame, in a movie clip, where? If you put it in the button, it won't work.

    You need to make sure that it's re-executing every frame, so it needs to go in one of these. Forgive me if I'm pointing out the obvious...

    onClipEvent(enterFrame) {

    --Code Here--

    }

  8. #8
    Senior Member
    Join Date
    Mar 2002
    Posts
    183
    OK, about the onclipevent (enterframe) {
    I actually tried that, then I got an output error saying that it cannot work like that. However, when the code is not in an onclipevent, I get an error that says that the code needs to be in an on or onclip event!!!! So now, I made a seperate movie clip named "Finishline" and gave it the instance name of line. Then I was trying a hittest between the horse and the line. Still hasn't worked. Here is my exact code up until now.
    Frame 1:
    Stop()
    this is just a button that when on click gotoandplay the race.
    Frame 2:
    if (_root.horse1.hittest(line)) {
    gotoandplay(4)
    }

    if (_root.horse2.hittest(line)) {
    gotoandplay(5)
    }

    Frame 3: gotoandplay(2);
    Frame 4/5/6 : stop() these are just the winner screens.

    On each horse the following code:
    Frame 1:
    _x1=_x
    vel1=Math.random()*20

    _x+=(vel1^2)
    Frame 2: gotoandplay(1);

    and that is it. Maybe I am using hitTest wrong, I have never used it before. this seems like it should be pretty simple to do, what am I doing wrong?!?!
    Thanks for any help.

  9. #9
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    Ok, it sounds like you're using all of the right code in all of the wrong places. I very rarely put code into frames.

    The onclipevent(enterframe) only works when you put it into a movieclip instance.

    Select the instance, and put your code into the actions panel. Start from where you were before, there's no need to use the hittest, the if x>500 should be much faster and simpler.

    Also, you can just use _x instead of _root.horse1._x when the code is placed into the instance (it's like a relative address, instead of an absolute address)

    So:
    Frame1 --- your button thing, goes to frame 2 when pressed.
    Frame2 - stop() -- so it doesn't go straight to finish. ALL of the action will take place in frame 2.

    Then put horse1 and horse2 into frame2.

    Then put this code into the horse1:

    onClipEvent(enterFrame){
    _x1=_x
    _x2=_x
    vel1=Math.random()*20
    vel2=Math.random()*20
    _x+=(vel1^2)
    _x+=(vel2^2)
    if (_x>500){
    _root.gotoandplay(3);
    } }

    And this code into horse2: (exactly the same, with a different gotoandplay frame)

    onClipEvent(enterFrame){
    _x1=_x
    _x2=_x
    vel1=Math.random()*20
    vel2=Math.random()*20
    _x+=(vel1^2)
    _x+=(vel2^2)
    if (_x>500){
    _root.gotoandplay(4);
    } }

    Where frame 3 is horse1 winning, and frame 4 is horse2 winning.

  10. #10
    Senior Member
    Join Date
    Mar 2002
    Posts
    183
    Oh my god, it works!
    Thank you so much lesli. I am going to try and put a betting thing in there, and unfortunately will probably need your help with that too, haha
    Anyways, I was not aware that you would just put code on an "instance". I thought that you could just put the code on the symbol itself, or put it onto a keyframe or something like that. Well thanks again, I really appreciate it.

  11. #11
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    No problem.

    If you need any more help, just come and post in my forums.

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