How To ? Global Variable Explanation please
#1
Posted 30 August 2003 - 10:09 AM
first question to me....
I have been messing with a drawing script from FK tutorials, that uses this movie clip method -
MovieClip._xmouse to locate the position of the mouse
Now, in the help files, it indicates that this MovieClip method is not supported by Sprites, and should be, or is, emulated with global variable.
Can someone explain to me the way the global variable works, and how I would emulate this movie clip method of -
_xmouse to find the mouse location.......using a global variable??
david
NEW! Project Broadcast ...broadcast audio content via the flash player, be your own DJ.
#2
Posted 30 August 2003 - 10:25 AM
just use _root._xmouse and _root._ymouse to get the position of the mouse in your whole movie script. (SWF5+ only in export)
I haven't tried it yet, but MC's/sprites also have this property, even tho it says its not supported in the internal player, i can only assume its supported in export as the above. that will give you the position of the mouse relative to the origin of the sprite (or may always be the top left not sure).
i've tried it with dynamically generated TextFields and those _xmouse/_ymouse properties do work quite nicely, and position is relative to the textfield origin (dynamic generated text fields alwyas have origin at top left).
As to global variables, currently any variable is a global variable, tho Roger did say that support for local var defined variables in functions now works), so for any variable just access it using the full . path and it should work.
#3
Posted 30 August 2003 - 05:24 PM
This is an easy drawing script, put all code on the maintimeline.
parameters of LineStyle (pencil) are :
0=thickness
0x000000 (black)=colour
100=alpha
------------------------------------------------------------------------------------------
onLoad () {
_root.lineStyle(0, 0x000000, 100);
}
on (press) {
draw = true;
startX = _root._xmouse;
startY = _root._ymouse;
_root.moveTo(startX,startY);
}
on (release) {
draw = false;
}
onEnterFrame() {
if (draw) {
newX = _root._xmouse;
newY = _root._ymouse;
if ((newX != startX) or (newY != startY)) {
_root.lineTo(newX,newY);
// reset location for new time
startX = newX;
startY = newY;
}
}
}
-----------------------------------------------------------------------------------------------
Cheers
#4
Posted 30 August 2003 - 06:03 PM
Thanks for your explanation...I think I follow that (I also think I am so thick sometimes, a different story
Mario,
that works just beautifully
I think that together you have helped to clear this up a little for me.
And Mario, you have provided a nice easy script for users to do something that is fun to do.
Cheers to both of you,
david
NEW! Project Broadcast ...broadcast audio content via the flash player, be your own DJ.
#5
Posted 31 August 2003 - 05:33 PM
Fine, hope you can fix the problem with your empty sprite !!
This script would be better, if we can make the lines bend to hide the connected dots look.
Maybe someone can help us....
Cheers
#6
Posted 01 September 2003 - 03:29 AM
you want some suggestions on make the line curve smoothly.
here are two ideas that may help.
1) Penner's curvethru prototype as discribed in Moocks book pg649
convert to swishMAX
// curve through function
//use like this curveThru(_root.nameSprite,{x:1,y:2},{x:3,:y4},{x:5,y:6});
function curveThru(clip,p1,p2,p3){
var newp2={x:((2*p2.x)-.5*(p1.x+p2.x)),y:((2*p2.y)-.5*(p1.y+p2.y))};
clip.moveTo(p1.x,p2.y);
clip.curveTo(newp2.x,newp2.y,p3.x,p3.y);
}2) Lifaros curved poly shapes by using mid point of lines in Fresh Flash pg 61
convert to swishMAX (quite different looking from original)
// curve midpoints function
//use like this curveMid(_root.nameSprite,{x:1,y:2},{x:3,:y4},{x:5,y:6});
function curveMid(clip,p1,p2,p3){
var mid1={x:((p1.x+p2.x)/2),y:((p1.y+p2.y)/2)}
var mid2={x:((p2.x+p3.x)/2),y:((p2.y+p3.y)/2)}
clip.moveTo(mid1.x,mid1.y);
clip.curveTo(p2.x,p2.y,mid2.x,mid2.y);
}3)
maybe they can be put together, something like
function curveSmooth(clip,p1,p2,p3){
var newp2={x:((2*p2.x)-.5*(p1.x+p2.x)),y:((2*p2.y)-.5*(p1.y+p2.y))};
if (_root.mid1!=""){ var mid1={x:((p1.x+p2.x)/2),y:((p1.y+p2.y)/2)};}
var mid2={x:((p2.x+p3.x)/2),y:((p2.y+p3.y)/2)};
clip.moveTo(mid1.x,mid1.y);
clip.curveTo(newp2.x,newp2.y,mid2.x,mid2.y);
_root.mid1=mid2;
}
onEnterFrame() {
if (draw) {
newX = _root._xmouse;
newY = _root._ymouse;
if ((newX != startX) or (newY != startY)) {
curveSmoothly(this,{x:oldX,y:oldY}, {x:startX,y:startY}, {x:newX,y:newY});
// reset location for new time
oldX=startX;
oldy=startY;
startX = newX;
startY = newY;
}
}all code is untested, it may work but more than anything it is just an idea to try, maybe just stick with lifaros idea. anyway have fun and let us see how you go.
JLM
#7
Posted 02 September 2003 - 10:34 AM
Using the lifaros method it proves to be the same as a solution they posted (I think there version was a lot more verbose than the messy ver below) unfortunatly the idea of going through the points using curveThru makes rather a bitty curve.
This code works in flash (when you remove the onFrame(1) but not working in swishMAX maybe it is a bug will look later unless someone picks it up. I was going to post a revised mouse follower but it had problems so I posted the this since I know it works in flash....? I think I am going to make my Ttrace into a movie to save on pasting it in every movie that need trouble shooting. Anyway;
function curveSmooth(clip,p1,p2,p3){
if (_root.mid1!=""){ var mid1={x:((p1.x+p2.x)/2),y:((p1.y+p2.y)/2)}};
var mid2={x:((p2.x+p3.x)/2),y:((p2.y+p3.y)/2)};
clip.moveTo(mid1.x,mid1.y);
clip.curveTo(p2.x,p2.y,mid2.x,mid2.y);
_root.mid1=mid2;
}
onFrame(1){
cx = new Array(0, 40, 100, 200, 400, 450, 500, 550);
cy = new Array(200, 80, 100, 350, 20, 60, 250, 50);
this.lineStyle(0,0xff0000);
this.moveTo(cx[0],cy[0]);
for (j = 0; j < cx.length-2; j++) {
curveSmooth(_root, {x:cx[j], y:cy[j]}, {x:cx[j+1] ,y:cy[j+1]}, {x:cx[j+2], y:cy[j+2]} )
}
this.moveTo(cx[0],cy[0]);
for (j = 0; j < cx.length; j++) {
this.lineTo(cx[j],cy[j]);
}
}JLM
#8
Posted 02 September 2003 - 06:19 PM
Very explanative, thanks for your help.
But i´ve tried the code that you ´ve posted
and don ´t seems work for me.
Anyway thanks !
Cheers
#9
Posted 02 September 2003 - 06:40 PM
here's a link of what the code does in flash (removed onFrame(1)). It should work nearly the same in swishMAX, I will trouble shoot it later if you don't get it sorted before then.
http://www.justinfro...curveSmooth.swf
You might want to look at
http://www.were-here.com/forum/tm.asp?m=12...tmode=1&smode=1
JLM
#10
Posted 03 September 2003 - 02:41 PM
not sorted it yet?
There was a bug with the object and a change of "" to undefined required. I have modified the method to use arrays instead which prob makes it faster but I do not like using [0] for x hence originally used objects anyway the code you wanted, probably can be improved.
function curveSmooth(clip,p1,p2,p3){
if (_root.mid1!=undefined){ var mid1=[((p1[0]+p2[0])/2),((p1[1]+p2[1])/2)]};
var mid2=[((p2[0]+p3[0])/2),((p2[1]+p3[1])/2)];
clip.moveTo(mid1[0],mid1[1]);
clip.curveTo(p2[0],p2[1],mid2[0],mid2[1]);
_root.mid1=mid2;
}
onLoad () {
_root.lineStyle(0, 0x000000, 100);
draw = false;
}
on (press) {
draw = true;
startX = _root._xmouse;
startY = _root._ymouse;
_root.moveTo(startX,startY);
}
on (release) {
draw = false;
}
onEnterFrame() {
if (_root.draw) {
newX = _root._xmouse;
newY = _root._ymouse;
if ((newX != startX) or (newY != startY)) {//or (oldX!=startX) or (oldY!=startY))
var p1=[oldX,oldY];
var p2=[startX,startY];
var p3=[newX,newY];
_root.curveSmooth(this,p1,p2,p3);
// reset location for new time
oldX=startX;
oldy=startY;
startX = newX;
startY = newY;
}
}
}http://www.justinfro.../lineChaser.swf
JLM
#11
Posted 04 September 2003 - 06:31 PM
Sorry for the delay.
Excellent resolution.
Your function curveSmoth works very fine, thanks for your help.
Just that i wanted.
Cheers
#13
Posted 04 September 2003 - 07:32 PM
#14
Posted 04 September 2003 - 08:03 PM
Oh createEmptyMovieClip works fine now like you said I am happy about that
JLM

Help













