-
easy functions question
Say I have a button(named "butt"), a movie clip(named "thing") and a button in the movieclip (named "thingButt"). The only object on the stage is the "butt" with this script:
on(press){
_root.attachMovie("thing","newThing",1,{_x:200,_y: 200});
}
On the first frame of the main timeline is:
function move(target, horizontal, vertical) {
target._x = horizontal;
target._y = vertical;
}
On the "thingButt" inside the "thing" is this:
on(press) {
move(newThing, 300, 300);
}
In my mind, I press "butt" and "thing" appears. That works. When i press the "thingButt" it does not move the movie clip. I have tried using " this" as a target and _root.newThing, but nothing works. any suggestions?
Thanks
Jay
-
hi,
your 'move' function is defined in the main timeline, so you will need to target it from your button.
try ...
on(press) {
_level0.move(this, 300, 300);
}
cheers,
glenn
glenn mitchell
http://www.pixelassembly.com
-
Senior Member
First off, make sure you use _root.move(), since that is where move() is defined (see below for a more elegant way to do it).
Since 'thing' is the parent of 'thingButt', you should be able to do it this way:
code:
on(press) {
_root.move(_parent, 300, 300);
}
However, _root.newThing should have worked, also.
This will NOT work:
_root.move(newThing, 300, 300);
Because 'newThing' is not a child of 'thingButt'. Unqualified names are always assumed to be attached to the thing that the script is attached to (thingButt, in this case).
Here's the more elegant method. You define a movieclip.prototype function that will work for all movieclips.
In the _root script:
code:
MovieClip.prototype.move = function(h, v) {
this._x = h;
this._y = v;
}
Then the button code becomes:
code:
on(release)
{
_parent.move(300, 300);
}
If you still can't get it to work, upload your .fla file.
- Jim
-
glenn_a_m's way worked but I will try your way as well. I was wondering what is _parent and child? and what is the difference between a normal function and a prototype?
Thanks
Jay
-
Originally posted by inadaze
glenn_a_m's way worked but I will try your way as well. I was wondering what is _parent and child? and what is the difference between a normal function and a prototype?
Thanks
Jay
A parent is the movie clip the clip is in. In your example you have a button inside a movie clip, so the movie clip is it's parent. Think of it like a family tree, at the top is _root the daddy and mommy of everything, then from him you would have your movie clip being its "child" then from the movie clip you would have your button, being the "child" of movie clip and movie clip being its "parent"
-
hi,
A parent is the movie clip the clip is in. In your example you have a button inside a movie clip, so the movie clip is it's parent.
that's true, but when you use 'on(press) ' on a button, the scope (where the statements are executed), is actually the movieclip which contains the button (hence the use of 'this' and not 'this._parent' as the function's target parameter)
what is the difference between a normal function and a prototype?
a prototype adds to an Object Class (and is then available to any instance of that Class type)
so,
Code:
MovieClip.prototype.move = function(h, v) {
this._x = h;
this._y = v;
}
would add the 'move' function to ALL MovieClip instances
meaning that rather than having to directly target your move function on the main timeline, it is now available within any movieclip.
your button code would then be
Code:
on(release)
{
this.move(300, 300);
}
cheers,
glenn
glenn mitchell
http://www.pixelassembly.com
-
Thanks glenn, wasn't aware that the button AS actually ran at MC level.
Originally posted by glenn_a_m
hi,
that's true, but when you use 'on(press) ' on a button, the scope (where the statements are executed), is actually the movieclip which contains the button (hence the use of 'this' and not 'this._parent' as the function's target parameter)
a prototype adds to an Object Class (and is then available to any instance of that Class type)
so,
Code:
MovieClip.prototype.move = function(h, v) {
this._x = h;
this._y = v;
}
would add the 'move' function to ALL MovieClip instances
meaning that rather than having to directly target your move function on the main timeline, it is now available within any movieclip.
your button code would then be
Code:
on(release)
{
this.move(300, 300);
}
cheers,
glenn
-
is it possible that if my function has an if statement that is ment to loop until on object reaches a specific size, that if I have a button that executes the function, it will not keep looping. Because my button, when pressed, makes the object grow but only one loop. Every time I press it, the object grows another step but does not loop on its own.
Is this because its a button? Is there ways to execute a function by pressing a "button" of some sort and the function will loop the if statement until it is finished?
Thanks
Jay
-
Post the FLA file here so we can see what u want to do, but otherwise try using a for loop inside the if loop
e.g
code:
if (buttonwas pressed){
for (i = 0; i < 100; i ++){
i--;
this._width += 2;
this._height += 2;
}
}
Originally posted by inadaze
is it possible that if my function has an if statement that is ment to loop until on object reaches a specific size, that if I have a button that executes the function, it will not keep looping. Because my button, when pressed, makes the object grow but only one loop. Every time I press it, the object grows another step but does not loop on its own.
Is this because its a button? Is there ways to execute a function by pressing a "button" of some sort and the function will loop the if statement until it is finished?
Thanks
Jay
-
hi,
unfortunately while that type of code will increase the size of the clip, you wont actually see it 'grow', you will only see the end result.
the stage is not updated while inside actionscript blocks
there are a couple of ways you could get the clip to 'grow', most notably using 'onEnterFrame' or 'setInterval'
personally, I like using setInterval, it's much more flexible (onEnterFrame is locked to the frameRate and there can only be one of them defined for any movieclip instance)
Code:
MovieClip.prototype.growTo = function(width, height, steps,speed) {
// width - the required _width
// height - the required _height
// steps - the required number of steps
// speed - the number of milliseconds per step
// the change in width per step
var wstep = (width - this._width)/steps;
// the change in height per step
var hstep = (height - this._height)/steps;
// i is a counter
var i = 0;
// because we lose 'this' inside the next function
// we create a reference to it
var ths = this;
// set a function to be called regularly with set interval
var intv = setInterval(
function(){
ths._width += wstep;
ths._height += hstep;
i++;
// clear the interval (intv)
// when we've done the number of steps required
if (i >= steps) clearInterval(intv);
},speed);
};
// and so, on your button, you could have ...
// (10 steps, 50 milliseconds each)
on(release)
{
this.growTo(300, 100,10,50);
}
cheers,
glenn
glenn mitchell
http://www.pixelassembly.com
-
was too tired to post the setinterval command glad the glenn sorted it out lol... hey give me a break it is like 5 in the morning *sips ground cafffffffine* yack tasts like something that came from the back of a zebrite
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|