8 Replies - 6010 Views - Last Post: 19 June 2011 - 08:31 PM

#1 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Passing button to external class

Posted 18 June 2011 - 10:41 PM

Hi there,

I am new to Flash and am looking for some help with linking a class I made to a bunch of buttons that I have on the stage. I had it working with repeating the code for each button. The code is to make each button shake. What I am trying to do, is make the "button effect" into a class and link it to each button. I have linked each button by selecting "export for actionscript" but I am not able to link all of the buttons to the same class(ShakeButton)

The buttons are named shaker_mc -shaker_mc7 (8 buttons)

Here is the code that I have in the ShakeButton.as file
package{  

 
import flash.display.Sprite;  
import flash.events.MouseEvent;  
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;



public class ShakeButton extends Sprite {  
private var shaker_mc = shaker_mc;
private var coordX:Number = shaker_mc.x;    
private var coordY:Number = shaker_mc.y;  
private var timer:Timer = new Timer(10);  



function ShakeButton():void {  

	shaker_mc.buttonMode = true; 
	coordX = this.x;    
	coordY = this.y;  
	timer = new Timer(10);  

	this.addEventListener(MouseEvent.ROLL_OVER,startShake);  
	this.addEventListener(MouseEvent.ROLL_OUT,stopShake);  
	timer.addEventListener(TimerEvent.TIMER, shakeImage);  

}  



function startShake(e:MouseEvent):void {  
	timer.start ()   
}  



function stopShake(e:MouseEvent):void {  
	timer.stop();  
	this.x = coordX;  
	this.y = coordY;  
	this.rotation = 0;  
}  



function shakeImage(event:Event):void {    
	this.x = coordX+ getMinusOrPlus()*(Math.random()*1);    
	this.y = coordY+ getMinusOrPlus()*(Math.random()*1);    
	this.rotation = getMinusOrPlus()* Math.random()*1;    
}    



function getMinusOrPlus():int {  

	var rand:Number = Math.random()*2;  
	if (rand<1) return -1  
	else return 1;  

		}  


	}  

} 






The code that I have on stage is

import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;

var shakebutton:ShakeButton = new ShakeButton();    

addChild(shakebutton); 




This is the error that I am getting

TypeError: Error #1010: A term is undefined and has no properties.
at ShakeButton()
at webbtn()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at main1_fla::MainTimeline()
TypeError: Error #1010: A term is undefined and has no properties.
at ShakeButton()
at main1_fla::MainTimeline/frame1()

I tried to load the .fla file, but I was unable to for some reason?

Any help would be great.

Thanks

This post has been edited by kurazi: 18 June 2011 - 11:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing button to external class

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Passing button to external class

Posted 19 June 2011 - 02:10 AM

You can try zipping the .fla file and then upload it. And do not open duplicate topics again.
Was This Post Helpful? 0
  • +
  • -

#3 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Passing button to external class

Posted 19 June 2011 - 07:01 AM

View Postchinchang, on 19 June 2011 - 02:10 AM, said:

You can try zipping the .fla file and then upload it. And do not open duplicate topics again.


Hey, sorry about that. My other post got deleted? I clicked on the link on the last post and it gave me this error -
[#10342] We could not determine which forum this topic is in. - which is why i posted a new topic.


I was able to attach the .fla file, as well as the ShakeButton.as

Thanks so much.
Was This Post Helpful? 0
  • +
  • -

#4 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Passing button to external class

Posted 19 June 2011 - 08:44 AM

Your .fla file is CS4 version and I am having CS3 right now here...so couldn't open it. But the main problem is in your linking of symbols.

First of all, remove the line 13 from ShakeButton.as :
private var shaker_mc = shaker_mc;



This is not required. You didn't understood the meaning of the external ShakeButton class and the linking.

What we are doing here is because we have same code for the buttons, so we collect that common code and put in a separate class called ShakeButton. After that we need to tell the buttons that are on the stage to inherit ShakeButton so that all buttons get the same functionality.

You don't need the line 13 because once you base all your buttons to a class, they are accessible using the this pointer. So wherever you use shaker_mc, replace it with this.

Now as far as the linking goes, read here about how To export a symbol for Actionscript. In the Base Class field put ShakeButton.

This post has been edited by chinchang: 19 June 2011 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

#5 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Passing button to external class

Posted 19 June 2011 - 10:40 AM

Hey, Thanks for your help.

I do have the base class set with the class name ShakeButton, and the class names for each button, is just their button name. Just like the article states, however it doesnt make a difference.

I removed that line of code you advised me to, as I think I accidently just left it when I was trying a few different things out. Now I have the 1120: Access of undefined property shaker_mc.
error.

I dont have any code for the buttons yet on the stage, they just have their instance names. I think that might be where the problem lays, I wasnt sure what come to write to access them. At some point, they are just going to be links that should load a movie clip.
Was This Post Helpful? 0
  • +
  • -

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Passing button to external class

Posted 19 June 2011 - 12:39 PM

View Postkurazi, on 19 June 2011 - 11:10 PM, said:

Hey, Thanks for your help.

I do have the base class set with the class name ShakeButton, and the class names for each button, is just their button name. Just like the article states, however it doesnt make a difference.

I removed that line of code you advised me to, as I think I accidently just left it when I was trying a few different things out. Now I have the 1120: Access of undefined property shaker_mc.
error.

I dont have any code for the buttons yet on the stage, they just have their instance names. I think that might be where the problem lays, I wasnt sure what come to write to access them. At some point, they are just going to be links that should load a movie clip.


1120 Error means you are still using the variable shaker_mc somewhere. I guess you didn't my last post completely. Go over it again.
Was This Post Helpful? 0
  • +
  • -

#7 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Passing button to external class

Posted 19 June 2011 - 01:38 PM

Quote

1120 Error means you are still using the variable shaker_mc somewhere. I guess you didn't my last post completely. Go over it again.


My apologies, I did read it over, but I assume the this would be used in the actionscript timeline code.

I have changed everything to this in the ShakerButton.as file and its great, but it looks like when I change these 2 lines
private var coordX:Number = shaker_mc.x;
private var coordY:Number = shaker_mc.y;

to

private var coordX:Number = this.x;
private var coordY:Number = this.y;

I get this error
Line 13 1119: Access of possibly undefined property x through a reference with static type Class.
Line 14 1119: Access of possibly undefined property y through a reference with static type Class.

Any ideas why I would get this?

Thanks a bunch

This post has been edited by kurazi: 19 June 2011 - 01:39 PM

Was This Post Helpful? 0
  • +
  • -

#8 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Passing button to external class

Posted 19 June 2011 - 08:05 PM

Just an update, I tried:

private var coordX:Number;
private var coordY:Number;

which is what I have been told to try by a few people, but that doesnt seem to be working either :(

Im sorry to be so annoying, but I am really just trying to wrap my head around this
Was This Post Helpful? 0
  • +
  • -

#9 kurazi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 27-March 11

Re: Passing button to external class

Posted 19 June 2011 - 08:31 PM

disregard!!

It did work!! I just had to relink it, it came off for some reason.

Thank you so much for all of you help!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1