Deep cloning using write byteArray not working - actionscript-3

I'm writing a clone function for a class of mine.
var buffer:ByteArray = new ByteArray();
buffer.writeObject(this);
buffer.position = 0;
var gameblock:* = buffer.readObject();
Now at the last line when the time comes to read the object. I get these three errors together:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. TypeError: Error #1034: Type Coercion failed: cannot
convert Object#c60efe9 to model.BlockData. TypeError: Error #1034:
Type Coercion failed: cannot convert Object#c5141c1 to
flash.geom.Matrix.
The class 'this' contains a user defined class BlockData and a Point . The errors are coming on that. How do you think I should clone this class?
I do overwrite the BlockData and the point again to make sure they get returned properly

Check this answer for better copy method with using of registerClassAlias method, but remember, that you can use this method only for simple cases, for example copy of data objects like TextFormat or Value Objects, you can't copy DisplayObject and its successor.

Related

TypeError: Error #1009: Cannot access a property or method of a null object reference. at AoW_2_fla::MainTimeline/frame1()

Trying to do an assignment for school and I keep constantly getting this error and I don't know why at all and neither does my teacher. The first part of the code works fine but once its get to
fbe_mc.addEventListener(MouseEvent.CLICK, gofbe);
function gofbe(e:MouseEvent):void{
gotoAndStop("fbetext1");
}
It won't function properly and gives the error below in the output. As3 class is fbetext and instance name is fbe_mc and the label is fbetext1. I seem to be the only one having this issue in my class so help would be appreciated. Changed code accidentally had extra pieces that I forgot to delete
TypeError: Error #1009: Cannot access a property or method of a null object reference. at AoW_2_fla::MainTimeline/frame1()
import flash.events.MouseEvent;
stop();
home_mc.addEventListener(MouseEvent.CLICK, gotoch1);
function gotoch1(e:MouseEvent):void{
gotoAndStop("Chapter 1");
}
fbe_mc.addEventListener(MouseEvent.CLICK, gofbe);
function gofbe(e:MouseEvent):void{
gotoAndStop("fbetext1");
}

AS3 Dynamic access to a sound channel

I have a flash project with many soundchannels active at the same time. I want a little function to play one and loop it, and the name of the sound channel will be passed as a parameter. This is the function:
function playBGMusic(channel:String):void
{
SoundChannel(channel) = bgSound1.play();
SoundChannel(channel).addEventListener(Event.SOUND_COMPLETE, loopBGMusic);
}
playBGMusic("bgChannel1");
This doesn't works, flash gives me this error:
1105: Target of assignment must be a reference value.
I tried to simplify the function, using an static string only in the listener
function playBGMusic():void
{
bgChannel1 = bgSound1.play();
SoundChannel("bgChannel1").addEventListener(Event.SOUND_COMPLETE, loopBGMusic);
}
playBGMusic();
This time it compiles, but it gives me this error:
Error #1034: Type Coercion failed: cannot convert "bgChannel1" to
flash.media.SoundChannel.
How can I access to a sound channel from a string?
Thaks.
You need to access it in the context of this:
SoundChannel(this[channel]) or SoundChannel(this["bgChannel1"])

Error #1034: Type Coercion failed: cannot convert ... to flash.display.MovieClip

I'm new to ActionScript, so there is a possibility that I'm asking something simple.
import flash.display.MovieClip;
var WinsRM:Array = new Array (protomanwin);
var Robotmaster:MovieClip = new MovieClip;
Robotmaster = WinsRM[0];
addChild(Robotmaster);
Robotmaster.y = 250;
Robotmaster.x = 70;
No compiler error but I get this error:
TypeError: Error #1034: Type Coercion failed: cannot convert protomanwin$
to flash.display.MovieClip.
The protomanwin is a movie clip with many frames. I don't know what's wrong. No symbol will display. Also I use an array because I want to have more than one symbol to call in the future.
It seems that "protomanwin" is a name of an asset in your library. In this case, "protomanwin" is type Class, so instead of doing Robotmaster assignment as you do right now, you have to do this:
Robotmaster=new WinsRM[0]();
Note that you can make more than a single instance of your movie clip should you need to.

Actionscript 3.0 type downcast issue

I have implemented a new class that extends MovieClip. It's name is base.MovieClipWithDelays ("base" here is a package name).
My scene contains such an object named Blah.
In Symbol Properties I checked Export for ActionScript and Export in first frame checkboxes.
I set Class Name as T_Idle_0.
And I specified it's Base class as base.MovieClipWithDelays.
The problem is that following code leads to the Type Error:
var dob:DisplayObject = getChild("Blah");
trace("SuperClass = " + getQualifiedSuperclassName(dob));
return MovieClipWithDelays(dob);
it outputs:
SuperClass = base::MovieClipWithDelays
TypeError: Error #1034: Type Coercion failed: cannot convert T_Idle_0#1ec59e9 to base.MovieClipWithDelays.
As you can see, it's superclass name is OK. Nevertheless, it fails to downcast it. How is it possible and how do I workaround it?
You cannot set the base class of a library MovieClip to a custom class. You can either set it to Sprite or MovieClip. To do what you want to do, you have two solutions:
1 . Manage everything (drawing, etc.) from your MovieClipWithDelays class. i.e. don't make it depend on a library object.
Or:
2 . Make your MovieClipWithDelays wraps a MovieClip instance.
var libraryMC:MovieClip = new SomeLibraryMovieClip();
var customMc:MovieClipWithDelays = new MovieClipWithDelays(libraryMC);
Then within MovieClipWithDelays, you'll need to have some function and properties to handle the wrapped MovieClip.

AS3 JSON.decode throwing #1009 error

I am using the as3corelib JSON library and decoding some JSON from a URLLoader request. However, I'm having issues with JSON.decode throwing an error:
TypeError: Error #1009: Cannot access a property or method of a null
object reference. at
com.adobe.serialization.json::JSONTokenizer/nextChar()
at
com.adobe.serialization.json::JSONTokenizer()
at
com.adobe.serialization.json::JSONDecoder()
at
com.adobe.serialization.json::JSON$/decode()
at Main/drawMap() at
flash.events::EventDispatcher/dispatchEventFunction()
at
flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
My code is as follows:
private function storeAssets(e:Event):void
{
// returned variables from PHP call
var variables:URLVariables = new URLVariables(e.target.data);
assets = JSON.decode(variables.assets);
}
I have passed my JSON input into validators and it always returns as valid so I'm really scratching my head on this.
Your right in putting e.target.data into the URLVariables, as per this example: http://actionscriptexamples.com/2008/02/27/decoding-url-encoded-strings-in-a-flash-application-using-the-urlvariables-class-in-actionscript-30/
What I believe is happening is that URLVariables is decoding your entire string into an object, thus variables.assets is not in JSON format because it has already been converted. It could also be that variables.assets is not defined in the return data.
Trace out your variables.assets and see if it is null, or not in JSON format.
I would use eithervar variables:URLVariables = new URLVariables(e.target.data) or assets = JSON.decode(e.target.data) but not both at the same time.