PDA

View Full Version : AS 3.0 Cookbook with bad examples???


Flash Gordon
06-03-2007, 03:07 AM
The below code is from the AS 3.0 Cookbook. They claim that I should be seeing lines on the stage. However, I don't see anything. I assume because the Sprite is not in the display list, therefore I can't see it. But this is one of the first examples the book has. I see nothing except the traces.

Ideas?


package com.flashgordon.drawling
{
import flash.display.Sprite;
import flash.events.Event;

public class Lines extends Sprite
{

public function Lines()
{
graphics.lineStyle(1, 0, 1);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}


private function onEnterFrame(event:Event):void
{
trace("running");
graphics.lineTo(Math.random() * 400, Math.random() * 400);
}
}
}

and here is another that does nothing for me, plus I get errors

package com.flashgordon.drawling
{
import flash.display.Sprite;
import flash.events.MouseEvent;

public class Lines extends Sprite
{
private var _sprite:Sprite;

public function Lines()
{
_sprite = new Sprite();
addChild(_sprite);
_sprite.graphics.beginFill(0xffffff);
_sprite.graphics.drawRect(0, 0, 400, 400);
_sprite.graphics.endFill();

_sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
_sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}

private function onMouseDown(event:MouseEvent):void
{
_sprite.graphics.lineStyle(1, 0, 1);
_sprite.graphics.moveTo(mouseX, mouseY);
_sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}

private function onMouseUp(event:MouseEvent):void
{
_sprite.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}

private function onMouseMove(event:MouseEvent):void
{
_sprite.graphics.lineTo(mouseX, mouseY);
}
}
}


Warning: 1090: Migration issue: The onMouseDown event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'mouseDown', callback_handler).

Warning: 1090: Migration issue: The onMouseUp event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'mouseUp', callback_handler).

Warning: 1090: Migration issue: The onMouseMove event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'mouseMove', callback_handler).

jimmy5804
06-03-2007, 04:52 AM
Which IDE are you using?

Flash Gordon
06-03-2007, 06:40 AM
Flash

wusj
06-03-2007, 07:26 AM
It works well in my Flex Builder 2,After I change something from your code.
package
{
import flash.display.Sprite;
import flash.events.Event;

public class ExampleApplication extends Sprite
{

public function ExampleApplication()
{
graphics.lineStyle(1, 0, 1);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}


private function onEnterFrame(event:Event):void
{
trace("running");
graphics.lineTo(Math.random() * 400, Math.random() * 400);
}
}
}

Flash Gordon
06-03-2007, 07:45 AM
yea i just don't see anything on the screen

import ExampleApplication;
new ExampleApplication();

plutocrat
06-03-2007, 08:02 AM
Right.

1. The second example is not wrong just coincidentally unfortunate; the CS3 IDE seems (I have found) terrified of any AS2 prowords. Change the onMouseMove, etc to other names

2.Your Lines class is fine, although I would change a few things in terms of coding practice: (a) always use hex colours and (b) use flash.events.TimerEvent rather than .Event to step your environment. In AS3 is just as easy as EnterFrame, and lets you tweak the interval independant of your framerate. Oh, and the package should be flashgordon.drawing, not .drawling. :)

The reason you are not seeing anything is that you have not added it to the display list. Your main timeline code (if you are adding it to the main timeline) should be:


import com.flashgordon.drawing.*

var lines:Lines = new Lines()
addChild(lines)

Flash Gordon
06-03-2007, 08:14 PM
thanks. The book left out how to make the class instance. addChild(lines) makes sesnse.

the CS3 IDE seems (I have found) terrified of any AS2 prowords. Change the onMouseMove, etc to other namesAnything besides changing my naming convention that I can do about this.

The IDE really sucks. Try to listen for KeyDown events in CS3 and it changes the IDE tools. The SWF won't pick it up until it is ran from outside the authoring envirnoment. Seems pretty stupid.

plutocrat
06-03-2007, 08:34 PM
They are trying to make migration easier buy making sure you do not accidentally believe that certain functions still exist, methinks. It is annoying that you can't turn it off (can you?).

Are textbooks worth it? I have learnt by making little flash toys. I imagine something I want, then I make it happen (like that lame Secret book...); thereby necessarily learning the structure of the platform as I go.

I wonder these things: I am a humanities (liberal arts on the other side of the pond) grad who has no formal programming background. I often wonder if other peoples' source code looks completely different...

Flash Gordon
06-04-2007, 03:38 AM
It is annoying that you can't turn it off (can you?).
Exactly how do you get rid of this stupid error message that isn't a error?
:mad:

EDIT: In the publish setting you can turn off error warnings.

Flash Gordon
06-04-2007, 06:13 AM
ok so lets try this one.....

package com.flashgordon.trace
{
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;


public class Example extends Sprite
{
private var prev:Number = 0;

public function Example()
{
var timer:Timer = new Timer(500, 10);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}

private function onTimer(event:TimerEvent):void
{
trace( flash.utils.getTimer() ); // <<--error here
//prev = flash.utils.getTimer();
}
}
}


1120: Access of undefined property flash.

Flash Gordon
06-04-2007, 06:39 AM
URGH...what the hell is wrong with this book?

On page 35, it states you don't need to define parameters if you want to pass an unlimited amount of arguments. Well that for sure isn't true.

Stupid O'Reilly

springframework
06-04-2007, 07:02 AM
the book is outdated ... or it would be if any other actionscript 3.0 book would be released.

Flash Gordon
06-04-2007, 07:08 AM
Any ideas about post # 10?

springframework
06-04-2007, 07:55 AM
you have to:

import flash.utils.getTimer;

Flash Gordon
06-04-2007, 08:10 AM
Thanks.

Guess what O'Reilly forgot to add at the top of the class ;) Don't these people even test the ephing code?

Okay, so unlike AS 2.0 where you could always use the fully qualified class name instead of importing, in AS 3.0 you ALWAYS must import every class you wish to use even if using the fully qualified name?

And why is getTimer() not apart of the flash.utils.Timer class? It should just be a static method. So what class does it belong to, no class? If no class association then why isn't it just top level like trace() or like it used to be.

Thanks for your time bro. This whole 2 -> 3 is quite a pain.

springframework
06-04-2007, 09:03 AM
this book was out before flash cs3 was released, so either they used flex builder 2 or flash 9 alpha to test their code on.

it is possible that it works in one of those environments.

Everyone seems to own this book, so i guess releasing untested code worked out for them ... since their isn't much competition it still holds as probably the best book.

june 15 is the reported release date for the book "essential actionscript 3.0"

dr_zeus
06-04-2007, 06:01 PM
Okay, so unlike AS 2.0 where you could always use the fully qualified class name instead of importing, in AS 3.0 you ALWAYS must import every class you wish to use even if using the fully qualified name?

Yes, all classes or functions, like getTimer(), must be imported.

And why is getTimer() not apart of the flash.utils.Timer class? It should just be a static method. So what class does it belong to, no class? If no class association then why isn't it just top level like trace() or like it used to be.

getTimer() is a function in the flash.utils package, but it is not part of a class. I imagine it isn't part of the Timer class because it has no direct relation to that class.

Mazoonist
07-08-2007, 02:19 AM
In the first example, in the calling FLA:

var myLines:Lines = new Lines();
addChild(myLines);


--OR--

In the fla, remove the above code, and:

Click the document to get document properties. In the Document Properties field, enter "Lines" (capital L, without the quotes, and assuming the file Lines.as is in the same folder, if not, you'll need to supply a path).

In either case, you'll get lines.

panel
07-08-2007, 08:53 AM
Actionscript 3.0 Cookbook was frist as3 book and was published very early, becouse it was based on some Beta os AS3 (propably flash player 8.5 Beta).

I have seen page containing list of all non working code from this book - there were many of examples (i just can't remember link). But even so i like structure of this book. Let's hope they will publish secound version.

davvolun
07-09-2007, 05:36 PM
Link to Actionscript 3.0 Cookbook errata (containing, as far as I've seen, most of the errors mentioned here, along with others)

http://www.oreilly.com/catalog/actscpt3ckbk/errata/

codelinker
07-11-2007, 07:23 AM
ok so lets try this one.....

package com.flashgordon.trace
{
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;


public class Example extends Sprite
{
private var prev:Number = 0;

public function Example()
{
var timer:Timer = new Timer(500, 10);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}

private function onTimer(event:TimerEvent):void
{
trace( flash.utils.getTimer() ); // <<--error here
//prev = flash.utils.getTimer();
}
}
}


1120: Access of undefined property flash.

try this

var prev:uint = getTimer();
trace (prev)

and also import 'import flash.utils.getTimer'

Flash Gordon
07-11-2007, 08:47 AM
Thanks guys for all the help, but I've moved passed the book now. This is kind of an old thread now. maybe I should lock it?