View Full Version : How to simply call a function?
pete66
May 4th, 2007, 07:55 AM
// OK, I write my function:
function CoordinatesArray(event:Event):void {
var xCoordinatesArray:Array = loader.data.xCoordinate.split(",");
var yCoordinatesArray:Array = loader.data.yCoordinate.split(",");
trace("The first number in the xCoordinates array is: " + xCoordinatesArray[0]);
trace("The first number in the yCoordinates array is: " + yCoordinatesArray[0]);
}
// Now, I want to run it (once)...
CoordinatesArray();
// Cheers!
duncanhall
May 4th, 2007, 08:06 AM
Unless something major has changed in AS3, I can't see any problem. You've defined your function, then you've called it?
Dazzer
May 4th, 2007, 08:20 AM
Well that won't work. He hasn't passed it an event.
duncanhall
May 4th, 2007, 08:22 AM
No, it wont work fully, but it's still calling the function, and you should still be seeing the trace action to confirm the function call?
Dazzer
May 4th, 2007, 08:28 AM
maybe he was writing a tutorial? lol
pete66
May 4th, 2007, 08:32 AM
maybe he was writing a tutorial? lol
Haha, it's funny you say that because I actually was! But not for you guys, I'm trying to help my brother make a series of graphs that use coordinates from an external txt file..
The function isn't getting called though. What event should I be attaching to it for it to just run once?
Thanks.
pete66
May 4th, 2007, 09:27 AM
CoordinatesArray();
The error it's giving me, by the way is:
1136: Incorrect number of arguments. Expected 1.
pete66
May 4th, 2007, 09:30 AM
Well that won't work. He hasn't passed it an event.
Please give an example? Thank you!
Aquilonian
May 4th, 2007, 10:16 AM
This function will receive an event object as argument, so its seems to be a listener
It will not be called just once, but whenever the event is dispatched
Aquilonian
May 4th, 2007, 10:19 AM
If you want it to be called just once,without listening to any event, remove the (event:Event)
and put ()
pete66
May 4th, 2007, 10:53 AM
If you want it to be called just once,without listening to any event, remove the (event:Event)
and put ()
Cheers mate
dthought
May 4th, 2007, 11:17 AM
Unless your function is destined to become a class, it's recommended not to capitalise the first letter...
pete66
May 4th, 2007, 01:25 PM
OK, I think I've pretty much finished my example tutorial for my brother...
He's very new to flash so there's lots of obvious comments in there, would be great to hear what any of the experienced people here think of it (I've been away from Flash (doing 3D) since version 5).
One thing I'm entirely not sure about is where and when a preloader for this sort of thing would be necessary, and how I'd go about implementing one?
Thanks again, this forum is awesome!
var stevesGraph:Shape = new Shape();
var thisGraphNum:Number = 0;
var translatedXPos:Number;
var translatedYPos:Number;
//LOAD YOUR DATA FROM EXTERNAL .TXT FILE
var request:URLRequest = new URLRequest("graphCoordinates.txt");
var loader:URLLoader = new URLLoader();
loader.load(request);
//Default dataFormat for imported data is TEXT. We want VARIABLES so...
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
//This is a function to format the data and make a graph
function drawGraph(event:Event):void {
//clear any previous graphs
stevesGraph.graphics.clear();
//This is to create an array from the imported data.
//so, xCoordinatesArray[0], xCoordinatesArray[1], xCoordinatesArray[2] etc.
//and... yCoordinatesArray[0], yCoordinatesArray[1], yCoordinatesArray[2] etc.
var xCoordinatesArray:Array = loader.data.xCoordinate.split(",");
var yCoordinatesArray:Array = loader.data.yCoordinate.split(",");
//trace("The first number in the xCoordinates array is: " + xCoordinatesArray[0]);
//trace("The first number in the yCoordinates array is: " + yCoordinatesArray[0]);
//Now, to draw a graph from the arrays...
//This is using Flash API (Application Programming Interface).
//
//Convert imported text into numbers (so we can apply math)
var convertMeX:Number = (xCoordinatesArray[thisGraphNum]);
var convertMeY:Number = (yCoordinatesArray[thisGraphNum]);
// this is to resize the graph because the outlines on the stage are 200x200,
// but all the numbers in the array go up to about 50)
convertMeX = convertMeX * 4;
convertMeY = convertMeY * 4;
//Convert our numbers relative to the position of the graph
//keep in mind, the top left of the screen is 0,0
translatedXPos = 200+ convertMeX;
translatedYPos = 600- convertMeY;
//this is the same as (thisGraphNum = thisGraphNum + 1)
thisGraphNum++;
if ((thisGraphNum) <= xCoordinatesArray.length){
// declare red line, 0.75 opaque (otherwise the line would be invisible)
stevesGraph.graphics.lineStyle(2, 0x990000, .75);
stevesGraph.graphics.moveTo(200, 600); //this is effectively 0,0
stevesGraph.graphics.lineTo(translatedXPos, translatedYPos);
//put that **** on the stage
this.addChild(stevesGraph);
//show the coordinates in text format
coordinatesText.text = convertMeX +", "+ convertMeY;
} else {
coordinatesText.text = "No more data available";
}
}
this.makeGraph_butt.addEventListener(MouseEvent.CL ICK,drawGraph);This is what's in the external .txt file example:
xCoordinate=21,45,33,11&yCoordinate=34,17,11,13
ajcates
May 4th, 2007, 03:00 PM
also if you just want to call the function lose the even part and just call it as normal, so you would write out the function like so
function CoordinatesArray():void {
var xCoordinatesArray:Array = loader.data.xCoordinate.split(",");
var yCoordinatesArray:Array = loader.data.yCoordinate.split(",");
trace("The first number in the xCoordinates array is: " + xCoordinatesArray[0]);
trace("The first number in the yCoordinates array is: " + yCoordinatesArray[0]);
}
and call it like
CoordinatesArray();
pete66
May 4th, 2007, 03:25 PM
also if you just want to call the function lose the even part and just call it as normal, so you would write out the function like so
ActionScript Code:
function CoordinatesArray():void {
var xCoordinatesArray:Array = loader.data.xCoordinate.split(",");
var yCoordinatesArray:Array = loader.data.yCoordinate.split(",");
trace("The first number in the xCoordinates array is: " + xCoordinatesArray[0]);
trace("The first number in the yCoordinates array is: " + yCoordinatesArray[0]);
}
and call it like
ActionScript Code:
CoordinatesArray();
Aquilonian already pointed that out but thanks anyway. :)
While we're on the topic I would like to understand a little more about what (event:Event) does? Then again, I suppose that's what the Flash Documentation is for. :ponder:
Aquilonian
May 4th, 2007, 03:56 PM
Events are the kind of objects that store information about things that happen
They are dispatched by another objects and sended to listener functions
Listener functions have to receive the event object as a parameter, and thats the goal of the (event:Event) in the function
You can find more about this in the AS3 tips of the day
Rezmason
May 4th, 2007, 10:13 PM
Also, if you have a function that you might want to respond to an event sometimes, and not other times, you can call it by saying
myFunction(null);
or by defining the function as such:
function myFunction(event:Event=null) {
...
}
The only time that this won't work is if your function actually uses the event passed to it. :)
pete66
May 5th, 2007, 01:09 AM
The only time that this won't work is if your function actually uses the event passed to it. :)
Heh, sounds like you figured that one out the hard way?
re: my actioncode a couple of posts back, can anyone enlighten me on how to tell if this would need a preloader to work online or whatever?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.