7 Replies - 2353 Views - Last Post: 27 April 2011 - 08:43 PM

#1 Fuingurth   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

AS3 No errors function doesn't work

Posted 27 April 2011 - 05:34 PM

Hello everyone. Thanks for taking the time to read this, and many thanks for helping me!

So, I am trying to use the setInterval() function in actionscript 3. I used this page as a reference, however I am doing it in AS3 rather than AS2 or AS1. I have created a red box on my first frame, and have converted it into a Movie Clip with the name box_mc. After clicking the box the setInterval function should make it move once every 1/2 second. Once its x position is at 300 it should stop moving. The code for the actions on this first frame can be seen below.

var box_mc:MovieClip=new MovieClip();
var myInterval;

box_mc.addEventListener(MouseEvent.CLICK, myFunction);
function myFunction(event:MouseEvent):void 
{
	box_mc.x += 10;
	if (box_mc.x>300) 
	{
		clearInterval(myInterval);
	}
}



This has not given me any errors, warnings, or output, however when I click on the box nothing happens. So I assumed that something is wrong with either 1) the click function... or 2) the box itself not being recognized or moved or something. I have tried renaming the box, using 'click' instead of 'MouseEvent.CLICK', and so many other things. So, as always I came to DIC to see if anyone could help me =D

Again, many thanks to all who look over this and offer help.

This post has been edited by Fuingurth: 27 April 2011 - 07:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: AS3 No errors function doesn't work

#2 Prototypical   User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: AS3 No errors function doesn't work

Posted 27 April 2011 - 06:42 PM

I don't see where you are setting an interval at all.

Based on what I see here, that box will move 10 pixels every time you click on it.

But using a timer and eventhandler is probably the most common way of achieving what you want in AS3 :


var boxTimer:Timer = new Timer(100, 0); // 100 milliseconds is 1/10th of a second.
boxTimer.addEventListener(TimerEvent.TIMER, timerHandler);
boxTimer.start();
			
private function timerHandler(e:TimerEvent):void 
{
        box_mc.x += 10;
	if (box_mc.x > 300)
	{	
	    boxTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
	}
}



The first parameter when creating the timer instance is the delay between timer events -- you can set that based on what you want. it's in milliseconds

Timer(delay, repeat);

so if you set repeat to 0, it'll repeat indefinitely. Otherwise it'll repeat the specified amount of times.

This post has been edited by Prototypical: 27 April 2011 - 06:47 PM

Was This Post Helpful? 1
  • +
  • -

#3 Fuingurth   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Re: AS3 No errors function doesn't work

Posted 27 April 2011 - 07:16 PM

WOW! ok, so for some reason, I was looking at another actionscript file and assumed that I moved over everything in the correct order. Apparently I left out the myInterval = setInterval(moveBox, 500);. However, after updating the code I have this.
var box_mc:MovieClip=new MovieClip();
var myInterval;

box_mc.addEventListener(MouseEvent.CLICK, myFunction);
function myFunction(event:MouseEvent):void 
{
	myInterval = setInterval(moveBox, 500);
}

function moveBox()
{
box_mc.x += 10;
	if (box_mc.x>300) 
	{
		clearInterval(myInterval);
	}
}

For some reason this still does not work :/
Also, thank you for showing me how to use the timer and event handler. However, again I have no result when I test, debug, export as .swf, etc. So, I modified it to run once the button is clicked... Still nothing is happening.
var box_mc:MovieClip=new MovieClip();
var boxTimer:Timer = new Timer(100, 0); // 100 milliseconds is 1/10th of a second.
boxTimer.addEventListener(TimerEvent.TIMER, timerHandler);
box_mc.addEventListener(MouseEvent.CLICK, myFunction);

function myFunction(event:MouseEvent):void 
{
	boxTimer.start()
}
	             
function timerHandler(e:TimerEvent):void
{
        box_mc.x += 10;
    if (box_mc.x > 300)
    {  
        boxTimer.removeEventListener(TimerEvent.TIMER, timerHandler);
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 Prototypical   User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: AS3 No errors function doesn't work

Posted 27 April 2011 - 07:25 PM

if box_mc is on your stage, why are you creating a new one in code ?

That is most likely why your click handler is not working. You create box_mc -- a new movieclip and it's not added to the stage or defined in anyway. How can you click on it ??

Remove that line and as long as you have an instance called "box_mc" on your stage, this should work.

This post has been edited by Prototypical: 27 April 2011 - 07:27 PM

Was This Post Helpful? 1
  • +
  • -

#5 Fuingurth   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Re: AS3 No errors function doesn't work

Posted 27 April 2011 - 07:39 PM

View PostPrototypical, on 27 April 2011 - 07:25 PM, said:

if box_mc is on your stage, why are you creating a new one in code ?

That is most likely why your click handler is not working. You create box_mc -- a new movieclip and it's not added to the stage or defined in anyway. How can you click on it ??

Remove that line and as long as you have an instance called "box_mc" on your stage, this should work.


I have that line because without it I get an error "access of undefined property". If I remove it from the stage, and then have that line, would I do something like this?
var box_mc:MovieClip=new MovieClip();
this.addChild(box_mc);
			box_mc.width=40;
			box_mc.height=40;
			box_mc.x = 50;
			box_mc.y = 50;

This post has been edited by Fuingurth: 27 April 2011 - 07:39 PM

Was This Post Helpful? 0
  • +
  • -

#6 Prototypical   User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: AS3 No errors function doesn't work

Posted 27 April 2011 - 07:53 PM

is it a named instance on your stage ? Are you on the frame with that instance when this code is executed?

The code you just listed will not create a box.

Do you know how to name an instance on the stage?
Was This Post Helpful? 1
  • +
  • -

#7 Fuingurth   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Re: AS3 No errors function doesn't work

Posted 27 April 2011 - 08:12 PM

View PostPrototypical, on 27 April 2011 - 07:53 PM, said:

is it a named instance on your stage ? Are you on the frame with that instance when this code is executed?

The code you just listed will not create a box.

Do you know how to name an instance on the stage?


Ah that was the problem, it didn't have an instance name. My properties window was closed, I completely forgot about that. Thank you so much! I knew it was some simple problem that I was just overlooking. So many thanks
Was This Post Helpful? 0
  • +
  • -

#8 Prototypical   User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 137
  • Joined: 20-April 11

Re: AS3 No errors function doesn't work

Posted 27 April 2011 - 08:43 PM

No problem, glad you have it worked out.

Best to deal with the errors and figure out what the problem is, instead of trying another way just to escape it!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1