Skip to main content
deborahb44958436
Inspiring
September 3, 2015
Answered

nextFrame(); that doesn't stop at the next frame.

  • September 3, 2015
  • 2 replies
  • 989 views

I have this scenario when a ball is dropped into a bucket full of water. The bucket have 4 levels of water that are arranged in 4  frames nested inside the bucket. Using the hitTestObject I was hopping  that each time the ball fall through the water, the water level in the bucket will drop one level. But instead, it turns out that as long as the ball continue its way down, it will trigger the nextFrame and so it wont stop on the next frame but rather go through all frames. Any solution?

if (yesh8gBall21.hitTestObject(yesh8allBuckets1)) {

  yesh8allBuckets1.nextFrame();}

This topic has been closed for replies.
Correct answer kglad

This one works, only problem that it shots off  the event listener for the duration. So if I used it for other things I'll need to separate them with other listener/function.

import flash.events.MouseEvent;

import flash.events.Event;

stop();

button1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);

function mouseDownHandler1(event: MouseEvent): void {

  yesh8gBall21.gotoAndPlay(1);

}

this.addEventListener(Event.ENTER_FRAME, handle8Collision2);

function handle8Collision2(evt: Event): void {

  if (yesh8gBall21.hitTestObject(yesh8allBuckets1)) {

removeEventListener(Event.ENTER_FRAME, handle8Collision2);

  yesh8allBuckets1.nextFrame();

  }

}


then add your event listener when the ball is clicked:

import flash.events.MouseEvent;

import flash.events.Event;

stop();

button1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);

function mouseDownHandler1(event: MouseEvent): void {

  yesh8gBall21.gotoAndPlay(1);

this.addEventListener(Event.ENTER_FRAME, handle8Collision2);

}


function handle8Collision2(evt: Event): void {

  if (yesh8gBall21.hitTestObject(yesh8allBuckets1)) {

this.removeEventListener(Event.ENTER_FRAME, handle8Collision2);

  yesh8allBuckets1.nextFrame();

  }

}

p.s. you should also remove the event listener if the bucket is not hit, too.

2 replies

kglad
Community Expert
Community Expert
September 3, 2015

that partial snippet of code is inadequate to display the problem because it's probably in a loop of some type.

show the loop.

deborahb44958436
Inspiring
September 3, 2015

This is the entire code:

import flash.events.MouseEvent;

import flash.events.Event;

stop();

button1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);

function mouseDownHandler1(event: MouseEvent): void {

  yesh8gBall21.gotoAndPlay(1);

}

this.addEventListener(Event.ENTER_FRAME, handle8Collision2);

function handle8Collision2(evt: Event): void {

  if (yesh8gBall21.hitTestObject(yesh8allBuckets1)) {

  yesh8allBuckets1.nextFrame();

  }

}

kglad
Community Expert
Community Expert
September 3, 2015

you probably want to use something like the following, though i'm not sure because i expected a drag-and-drop.  but maybe there's other code causing yesh.. to move:

:

import flash.events.MouseEvent;

import flash.events.Event;

stop();

button1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler1);

function mouseDownHandler1(event: MouseEvent): void {

  yesh8gBall21.gotoAndPlay(1);

handle8Collision2();

}

function handle8Collision2(): void {

  if (yesh8gBall21.hitTestObject(yesh8allBuckets1)) {

  yesh8allBuckets1.nextFrame();

  }

}

Ned Murphy
Legend
September 3, 2015

You likely need something to disable or further hitTests while the ball is involved with the bucket.  When the ball ends its dealings then start the hitTest checking again for the next ball.