-
Senior Member
AS3 function call error?
can you see whats wrong here?
i get an error saying that i am not calling a function (drawSun) but i can see anything wrong with the code?
Code:
package {
import flash.display.*;
import flash.geom.*;
import flash.util.*;
import flash.events.*;
public class DynamicSun extends MovieClip {
public var r:int = 180
public var magnitude:int = 10
public var d_r:Number = Math.PI/180
public var m:Object = {x:275, y:200}
public var sun:MovieClip = new MovieClip()
public function DynamicSun() {
addEventListener(EventType.ENTER_FRAME, drawSun)
}
public function drawSun(e:Event){
sun.clear()
sun.beginGradientFill("radial", [0xffff00, 0xff9900], [80, 100], [128+Math.floor(Math.random()*magnitude), Math.floor(225+Math.random()*magnitude)], {matrixType:"box", x:m.x-r, y:m.y-r, w:r*2, h:r*2, r:0});
sun((Math.cos(0)*(r+Math.random()*magnitude))+m.x, (Math.sin(0)*(r+Math.random()*magnitude))+m.y)
for(var z:int = 0; z<=360; z++){
sun.lineTo((Math.cos(z*d_r)*(r+Math.random()*magnitude))+m.x, (Math.sin(z*d_r)*(r+Math.random()*magnitude))+m.y)
}
sun.endFill()
addChild(sun)
}
}
}
cheers,
zlatan
-
Senior Member
-
addEventListener needs to take an event type (as a string) and an object in which to execute the event handler. The object specified in the addEventListener function needs to have a function with the same name as the event type it is handling...and the object that is adding the event listener needs to implement EventDispatcher...
K.
-
Senior Member
not sure what you mean mate, the following works, i am calling the functions in the same way, without any strings:
Code:
package {
import flash.display.*;
import flash.geom.*
import flash.util.*;
import flash.events.*;
import flash.filters.BevelFilter
import flash.filters.DropShadowFilter
public var line:MovieClip = new MovieClip()
public var g:Graphics
public var down:Boolean = false
public var bg:MovieClip = new MovieClip()
public var bev:BevelFilter = new BevelFilter(2,45,0xffffff,0.5,0,0.5,4,4,5,3,"inner",false)
public var shad:DropShadowFilter = new DropShadowFilter(6,45,0,0.6,5,5,1,3,false,false,false)
public var filts:Array = new Array()
public var xPos:Number = 0
public var yPos:Number = 0
public var lastx:Number = 0
public var lasty:Number = 0
public var k:int = 0
public var bmd:BitmapData = new BitmapData(550, 400, false, 0xffffff)
public var bm:Bitmap = new Bitmap(bmd)
public class drawTest extends MovieClip {
public function drawTest() {
g = line.graphics
bg.graphics.beginFill(0xffffff, 100)
bg.graphics.moveTo(0, 0)
bg.graphics.lineTo(550, 0)
bg.graphics.lineTo(550, 400)
bg.graphics.lineTo(0, 400)
bg.graphics.lineTo(0, 0)
bg.graphics.endFill()
addChild(bg)
addChild(bm)
filts.push(bev)
filts.push(shad)
this.addEventListener(MouseEventType.MOUSE_DOWN, setDownTrue)
this.addEventListener(MouseEventType.MOUSE_UP, setDownFalse)
this.addEventListener(MouseEventType.DOUBLE_CLICK, clearLine)
this.addEventListener(MouseEventType.MOUSE_MOVE, drawLine)
line.filters = filts
addChild(line)
}
public function drawLine(e:Event){
if(down){
k++
if(k%4 == 2){
if(k == 8){
k = 0
}
lastx = (xPos + mouseX) / 2
lasty = (yPos + mouseY) / 2
g.curveTo(xPos, yPos, lastx, lasty)
xPos = mouseX
yPos = mouseY
}
}
}
public function setDownTrue(e:Event){
down = true
xPos = mouseX
lastx = mouseX
yPos = mouseY
lasty = mouseY
g.lineStyle(3+Math.floor(Math.random()*10),Math.round(Math.random()*0xffffff), 100)
g.moveTo(mouseX, mouseY)
//line.graphics.drawCircle(mouseX, mouseY, 50)
}
public function setDownFalse(e:Event){
down = false
bmd.draw(line, new Matrix())
g.clear()
}
public function clearLine(e:Event){
bmd.fillRect(new Rectangle(0, 0, 550, 400), 0xffffff)
}
}
}
zlatan
-
So all those event handlers specified in the drawTest function are firing correctly on the mouse events?
K.
-
Senior Member
-
Senior Member
addEventListener() is not the same in AS3 as in AS2
-
Originally Posted by dudeqwerty
addEventListener() is not the same in AS3 as in AS2
Yeah, so it would seem...well, I'm no help to you then...
K.
-
I found a lot of Errors. The best way to get the bugs, is to comment code out, untill no error message is been thrown. Then go backwards.
So here some suggestions:
public var sun:MovieClip = new MovieClip() is not the fine way. You better instance the MovieClip in your constructor.
sun.clear().....
You cannot draw directly to a MovieClip as in AS2,AS1. There is a new member called graphics from flash.display.Graphics in each instance of most DisplayObjects.
addChild(sun)
You add the child on any enterFrame Event ? Once is enough.
There is also a missing bracket in the code above (method drawSun isn't closed).
-
it looks like the 1st script and 2nd script are unrelated. However, having two scripts (1 working and 1 not) is infact fastest method for debugging... but the trick is to combine the "wanted" code with the "working" code until both are working well together ...then follow Andre's method to cut whatever is not really needed.
here they are combined and working;
Code:
package {
import flash.display.*; import flash.geom.*
import flash.util.*; import flash.events.*;
import flash.filters.BevelFilter;
import flash.filters.DropShadowFilter;
public var line:MovieClip = new MovieClip();
public var down:Boolean = false; public var sunny:Boolean = true;
public var bg:MovieClip = new MovieClip();
public var bev:BevelFilter = new BevelFilter(2,45,0xffffff,0.5,0,0.5,4,4,5,3,"inner",false);
public var shad:DropShadowFilter = new DropShadowFilter(6,45,0,0.6,5,5,1,3,false,false,false);
public var filts:Array = new Array();
public var xPos:Number = 0; public var yPos:Number = 0;
public var lastx:Number = 0; public var lasty:Number = 0;
public var r:int = 120; public var magnitude:int = 14;
public var d_r:Number = Math.PI/180;
public var m:Object = {x:240, y:180}
public var k:int = 0; public var z:Number = 0;
public var bmd:BitmapData = new BitmapData(500, 400, false, 0xffffff);
public var bm:Bitmap = new Bitmap(bmd);
public var g:Graphics;
public class drawTest extends MovieClip {
public function drawTest() {
g = line.graphics;
bg.graphics.beginFill(0xffffff, 100); bg.graphics.moveTo(0, 0);
bg.graphics.lineTo(500, 0); bg.graphics.lineTo(500, 400);
bg.graphics.lineTo(0, 400); bg.graphics.lineTo(0, 0);
bg.graphics.endFill();
addChild(bg); addChild(bm);
filts.push(bev); filts.push(shad);
this.addEventListener(MouseEventType.MOUSE_DOWN, setDownTrue);
this.addEventListener(MouseEventType.MOUSE_UP, setDownFalse);
this.addEventListener(MouseEventType.DOUBLE_CLICK, sunOff);
this.addEventListener(MouseEventType.MOUSE_MOVE, drawLine);
this.addEventListener(EventType.ENTER_FRAME, drawSun);
line.filters = filts; addChild(line);
}
public function toggleSun(e:Event) { return(true); }
public function drawLine(e:Event) {
if(down) { k++;
if(k%4 == 2) {
if(k == 8){ k = 0; }
lastx = (xPos + mouseX) / 2; lasty = (yPos + mouseY) / 2;
g.curveTo(xPos, yPos, lastx, lasty); xPos = mouseX; yPos = mouseY;
}
}
}
public function setDownTrue(e:Event) {
down = true;
xPos = mouseX; lastx = mouseX; yPos = mouseY; lasty = mouseY;
g.lineStyle(3+Math.floor(Math.random()*10),Math.round(Math.random()*0xffffff), 100);
g.moveTo(mouseX, mouseY);
}
public function setDownFalse(e:Event) {
down = false; bmd.draw(line, new Matrix()); g.clear();
}
public function sunOff(e:Event) {
sunny = false; bmd.fillRect(new Rectangle(0, 0, 550, 400), 0xffffff); g.clear();
}
public function drawSun(e:Event) {
if(sunny) {
// Gradient fill
var filltype:String = GradientType.RADIAL;
var colors:Array = [0xffff00, 0xff9900];
var alphas:Array = [80, 100];
var ratios:Array = [60+Math.floor(Math.random()*magnitude), Math.floor(240+Math.random()*magnitude)];
var pads:String = SpreadMethod.PAD;
var intrgb:String = InterpolationMethod.LINEAR_RGB;
var fpratio:Number = 0.2;
var matr:Matrix = new Matrix();
matr.createGradientBox(m.x+40, m.y+160, r-500, r, 0);
g.beginGradientFill(filltype, colors, alphas, ratios, matr, pads, intrgb, fpratio);
g.moveTo((Math.cos(0)*(r+Math.random()*magnitude))+m.x, (Math.sin(0)*(r+Math.random()*magnitude))+m.y);
for(var z:int = 0; z<=360; z++) { g.lineTo((Math.cos(z*d_r)*(r+Math.random()*magnitude))+m.x, (Math.sin(z*d_r)*(r+Math.random()*magnitude))+m.y); }
}
}
}
}
note: double-click for draw function only.
" This is the first step of a long journey." -andre michelle, Recycle.
-
Senior Member
i forgot the graphics class!!!!! arrggh how could i have missed that, lol.
also im a bit desgusted with the amount of mistakes i made lol
cheers everyone!
kidoo, cheers mate! lol but its no quite the effect i was going for, heres an AS2 example of it here,
but i think im going to incorporate some filters into it as well.
cheers again,
zlatan
Last edited by dudeqwerty; 01-27-2006 at 01:20 PM.
New sig soon
-
Senior Member
hey while on the subject of AS3 and the like, i hope you dont mind, but i have 2 more minor questions.
1. what is the best/most efficient method of refreshing a bitmap (like using mc.clear()) do you just fillrect() after each movement?
and the second, can i get flex to open the swf in flash player and not in a browser when i run my projects?
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
|