A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: AS2 When symbol is in a certain position - go to certain frame

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    26

    Post AS2 When symbol is in a certain position - go to certain frame

    Hi, I'm using Flash Professional CS5.5 and I made symbol (movie clip in frame 1) that I can move by keyboard via following script:

    onClipEvent (enterFrame) {
    if(Key.isDown(Key.RIGHT)) {
    _x += 10;
    }
    if(Key.isDown(Key.LEFT)) {
    _x -= 10;
    }
    if(Key.isDown(Key.DOWN)) {
    _y += 10;
    }
    if(Key.isDown(Key.UP)) {
    _y -= 10;
    }
    }

    Now, I want to make that aplication (timeline) goes to certain frame (gotoAndPlay(2)) when symbol is in a certain position (_x or _y coordinate). How can I make this? What do I need to type in actions field of frame 1? Or in actions filed of movie clip?

    Thank you

  2. #2
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    You can do this 2 different ways. If you place the code on the movieclip then you use either _root.gotoAndPlay(2) or _parent.gotoAndPlay(2). If you place the code on frame 1 then you need to give the movieclip an instance name. Either way you want to use something like:
    if(mc._x < 10) {
    _root.gotoAndPlay(2);
    }
    With mc referring to the movieclip. Remember also that the (0,0) point is at the top left of the stage.
    .

  3. #3
    Junior Member
    Join Date
    May 2019
    Posts
    26
    It doesn`t work. Please, can you write full code for both ways. Should I type onEnterFrame = function() or onClipEvent (enterFrame) before that if?

  4. #4
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    By itself the code won't work. It needs to be placed properly. When you put code on a frame in the timeline then it'll only run once. You'll want to put in in an enter frame event. Either way that you've asked should work. You could also add it below your moving code. You'd just need remove the "mc" part that I put in my example for it to work.

    One thing you can use for feedback to test your code is the trace() function. For example: trace(_x); will print the x in the output panel every time the function is run. It's a nice way to either test if code is running or to see it's output. It's a simple form of debugging.
    .

  5. #5
    Junior Member
    Join Date
    May 2019
    Posts
    26
    It works now. I placed following code on the movieclip:

    onClipEvent (enterFrame) {
    if(Key.isDown(Key.RIGHT)) {
    _x += 10;
    }
    if(Key.isDown(Key.LEFT)) {
    _x -= 10;
    }
    if(Key.isDown(Key.DOWN)) {
    _y += 10;
    }
    if(Key.isDown(Key.UP)) {
    _y -= 10;
    }
    if(_x > 200) {
    _parent.gotoAndPlay("name of certain frame");
    }
    }

    Thanks !

  6. #6
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    No problem. I like to help but I prefer directing a person to understand instead of just laying out the answer. This is actually kind of how I learned how to code. I basically grabbed code that looked like what I wanted then played with it to understand what was going on.
    .

  7. #7
    Junior Member
    Join Date
    May 2019
    Posts
    26
    OK, thanks again. But, is there some code that movie clip in frame 3 start from the same position (by _x coordinate) as it finished in frame 2? For example, player move that clip in frame 2 by keyboard ( if(Key.isDown(Key.RIGHT)) { _x += 10; ) and when he types ENTER application goes to the frame 3 where is the same movie clip but with different codes ( if(Key.isDown(Key.DOWN)) { _x += 20; etc... ), but it should start from the same position (_x) as it finished in frame 2. Is there some code for that?

  8. #8
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    In my experience, unless you have a simple project it's better to keep your code organized. With flash it's way to easy to scatter the code everywhere. In this case I suggest you add another layer that just contains the code and nothing else. Have that frame extend across the whole timeline. That way you can have persistent code. Then you can use a switch: https://en.wikibooks.org/wiki/Introd...rol_Statements. Here's an example:
    Code:
    switch(_currentframe){
         case 1:
         trace("frame 1");
         break;
         case 2:
         trace("frame 2");
         break;
         default:
         trace("runs if the code doesn't get stopped by previous breaks");
    }
    Back to your question. I'd suggest you use a variable to track the previous object's position to use to update the new one.
    .

  9. #9
    Junior Member
    Join Date
    May 2019
    Posts
    26
    Hm...it`s not a simple project (a lot of scenes, layers, movie clips and animations...) but I`m a simple AS2 coder I don`t know to use variable and that`s why I asked for code to track the previous object's position to use to update the new one. Can you give actual code?

  10. #10
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    https://www.dreamincode.net/forums/t...s-1-variables/ is a basic tutorial on variables for actionscript 2.0. In practice for you, you should create a variable and have it equal your mc's position: var mcx=mc._x; Then in the second frame you want the mc's position to be set based on that variable. Place this code outside of the enterframe function that you have set up. It only needs to run once. You can send me your fla file if you want me to take a look at it. Also, let me know how you'd prefer to correspond. Either here or pm is fine.
    .

  11. #11
    Junior Member
    Join Date
    May 2019
    Posts
    26
    https://we.tl/t-WRWn5Rdc77 is Test.fla You should move mc (green box) left/right by keyboard and when application goes from frame ''start'' to frame ''end'' by counter ( var counter = 0; ), green box should be in the same _x position where you moved it in frame ''start''. But, it doesen`t work, green box is always in starting position in frame ''end''.

  12. #12
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Depending on what you're trying to accomplish, you're making things rather complicated. Would you like to set up a chat session to talk this through? It would allow for me to talk you through this. The reason I say this is from the example you sent me it would be simpler to only use 1 frame. Using multiple frames is more for animating.

    Test.fla is a simple fix for what you want. I feel it's messy because the code will run continuously even when the code isn't needed.
    .

  13. #13
    Junior Member
    Join Date
    May 2019
    Posts
    26
    Great! That`s it. Yes, I understand your comment but this is just simple test for code that I`m looking for. In fact, I made more complex application with many animations and drawings, that`s why I used multiple frames. Thanks again. Here is the code, for others:

    var basemcx = Clip._x;
    var currentmcx = basemcx;
    onEnterFrame = function ()
    {
    if(Clip._x != basemcx){
    currentmcx = Clip._x;
    } else {
    Clip._x = currentmcx;
    }
    }

  14. #14
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Fair enough. BTW, on this forum you can encapsulate code by using the tag: [ CODE]put code here[/ CODE] with no extra spaces.

    Kind of a side note. How I would handle the text example would be to place all the code that controls the Clip in one place. In fact I would further separate things a bit further and keep updating Clip._x's position based upon currentmcx. Then I'd use the arrow keys to modify currentmcx. If you're interested in this I can alter update the fla with the concept. It should be fairly simple. I would post an update if I was more sure it'd be in line with what you're working with.

    Basically in my opinion it's better to keep everything grouped together as possible. The way you have it set up now you'll have to copy the control code with every new keyframe. Instead you could use something like an array to modify the movement speed based upon currentframe. The array would be used to determine what frame to change the speed and what speed to use. This way you can just add to the array if you want to modify it more.

    If you couldn't tell I started learning to code like how you have it. I found the experience rather distasteful and confusing. For more complex projects it grows increasingly complex and more likely that code might get misplaced. If you can handle it then inform me. Otherwise I'll keep mentioning suggestions.
    .

  15. #15
    Junior Member
    Join Date
    May 2019
    Posts
    26
    Well, it`s work for now but I`m beginner and I`m not sure will I have some problem with that code later. It would be very nice to update and post fla file with that new concept. You can update my test.fla?

  16. #16
    Junior Member
    Join Date
    May 2019
    Posts
    26
    Actually, it doesen`t work as well when I put that code in more complex application. I think I know why. In first frame MC should go in wrong (-) direction by key.RIGHT, and in second frame it should start from same position as it finished in first frame but now it should go in right (+) direction by the same key.RIGHT. I made that situation in simple test.fla file and code doesen`t wotk too.
    https://we.tl/t-O33iKVWxyb --> take a look, please.

  17. #17
    Junior Member
    Join Date
    May 2019
    Posts
    26
    Oh, no, this is not the right reason. Probably, code doesn`t work in more complex app because there are a lot of other codes in other layers ....

  18. #18
    Junior Member
    Join Date
    May 2019
    Posts
    26
    Uhh...I spent all day trying to find out the right reason... It works when are the same MC codes in both frames ( if(Key.isDown(Key.RIGHT)) {
    _x -= 5; ) but if codes are different ( _x -= 5 in first frame and _x += 8 in ''end'' frame ) it doesn`t work
    https://we.tl/t-qKijsEQP15 is the test app.fla. Please, can you take a look, green MC should start from the same position in frame ''end'' as it finished in first frame.

  19. #19
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    https://we.tl/t-HebVi9mfzY It's still wonky but I think this does more. In my opinion it's probably better to keep multiple frames contained in movieclips. This is actually the most I've done with adding code with multiple frames. I added a textfield to display the numbers.
    .

  20. #20
    Junior Member
    Join Date
    May 2019
    Posts
    26
    Yes, it`s better but that`s not it 100%. I see 2 problems here. First, movement of green object (_x) stops in first frame when it starts with animation. Second, movement in "end" frame doesn`t work at all. Green object should continue to move by key.RIGHT in ''end'' frame, continue to move in right (+) direction. In first frame, it moves in wrong (-) direction and that`s ok. In any case, thanks for help. If you find out even more better solution, it would be great.

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