Papervision rendering PNGs with transparency thats inside a movieclip - actionscript-3

I'm an actionscript dude - I'm working on a papervision game.
I have an asset of which is 127 pngs in a sequence for an animation.
I can happily project this onto my papervision plane. Problem is, there is no transparency. I Can't use a BitmapFileMaterial as I have many pngs -
can anyone suggest how to do this.
Very very grateful -

myMaterial.transparent = true
Or something like that, check out the docs if that doesn't work.

this is my code so far. A simplified version edit - Papervision 2.0.0
package com.strangemother.gameObjects
{
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
public class Biorod extends DisplayObject3D
{
/*
My flash movieclip with 127 pngs in sequence
*/
private var textureMC:BiorodTexture = new BiorodTexture();
private var movieMat:MovieMaterial = new MovieMaterial(textureMC, true, true)
var plane:Plane = new Plane(movieMat, 300,300,1,1);
public function Biorod()
{
textureMC.id = 'biorod';
movieMat.animated = true;
movieMat.doubleSided = true;
// movieMat.interactive = true;
movieMat.smooth = true;
movieMat.movieTransparent = true;
this.addChild(plane);
}
}
}

Reading over google - There seems to be a bug -
private var movieMat:MovieMaterial = new MovieMaterial(textureMC, true, true)
set to
private var movieMat:MovieMaterial = new MovieMaterial(textureMC, false, true)
and later setting
movieMat.movieTransparent = true;
seems to work.
Uber thanks for your help -pointed me in the right direction.

Related

Flex playing mp3 stream doesn't work

I've copied the sample code from Adobe for playing an mp3 stream in Flex mobile but it doesn't seem to work.
The stream i used as an example works perfectly fine in Winamp.
This is my code:
import flash.net.*;
import flash.media.*;
private var req:URLRequest;
private var context:SoundLoaderContext = new SoundLoaderContext(8000, true);
private var s:Sound;
private var channel:SoundChannel = new SoundChannel();
private function AudioOn():void
{
req = new URLRequest("http://stream2.srr.ro:8000");
s = new Sound(req,context);
channel=s.play();
}
private function onInit() : void {
AudionOn();
}
Using the debbuger the s (sound) object has the following state:
s.isBuffering is true;
s.isURLInaccesible is false;
s.bytesLoaded = 0
s.bytesTotal = 0;
This seems to be an easy task but why doensn't this example work ?
Thanks a lot!
Dan
Have you ever tried to modified your sound file by
Remove all metadata in the sound file.
Export it to a new MP3 file.
Is it too large to load? Try to add error event to listen.

Something like cookies in Flash/ActionScript

i need to implement something like a cookie in a flash file....and i dont have a clue about ActionScript.
Basically it is a video with a mute/unmute button. If i mute the video and refresh the browser it is not muted again.So i need to persist the mute status somehow.
Here is my complete ActionScript File:
import flash.net.SharedObject;
var a:Boolean = false;
var cookie:SharedObject = sharedobject.getLocal("muted");
if (cookie.data.muted == true) {
SoundMixer.soundTransform = new SoundTransform(0);
Object(root).ton_btn.gotoAndStop(2);
}
ton_btn.addEventListener(MouseEvent.MOUSE_OVER, fl_MouseOverHandler);
function fl_MouseOverHandler(event:MouseEvent):void
{
Object(root).ton_btn.buttonMode = true;
Object(root).ton_btn.useHandCursor = true;
}
ton_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
if (! a)
{
var muteStatus:Boolean = true;
cookie.data.muted = muteStatus;
SoundMixer.soundTransform = new SoundTransform(0);
Object(root).ton_btn.gotoAndStop(2);
trace(a);
}
else
{
var muteStatus:Boolean = false;
cookie.data.muted = muteStatus;
SoundMixer.soundTransform = new SoundTransform(1);
Object(root).ton_btn.gotoAndStop(1);
trace(a);
}
a = ! a;
}
This is not working, now my mute button is flickering....it seems, that the if clause is constantly executing.
Thanks for any tip, hint or link in advance. ;)
Regards
Nils
Edit:
So stupid...it was just a typo.
var cookie:SharedObject = sharedobject.getLocal("muted");
must be:
var cookie:SharedObject = SharedObject.getLocal("muted");
Now it is working.
I recommend following opensource that is popular ActionScript3.0 Cookie Frameworks. In general, two kinds of cookies in with the framework is known. Please see under open source. Solve your problem more quickly, you may give. Number 1 is a only Sourcode and Tutorial, Number 2 is a full of Frameworks.
Read and Write and Edit using a SharedObject
Actionscript3 Cookie Util

Game session Sound Playback / Recording

So I'm making a simple flash game. Basically I have a turntable that goes on for 30 seconds with a couple of sample music that adds up together in multiple layers of sound to form a final song.
Now I would like to be able to record and play the sounds at the end of the game.
I've created a SoundFx class that takes mp3 audio and turns it into byteArrays with the hope to mix the audios in to the same Sound channel.
Now I've reached a certain impass since I cannot properly mix the bytearrays. I'm starting to think it's not possible to encode the byte Arrays as you add the channels to the mix.
I'd love to be guided in the right direction. I'm not sure if the best way to proceed from here, even just the playback would be nice. Creating a button log would probably fix the playback and mixing the audio in a second run to go straight to the file. but it sure seems like a long path to achieve this.
Many thanks and apologies for my crappy english in advance
David R.
some code on the matter:
private var srcSound:Sound;
private var sound1:Sound;
private var sound2:Sound;
private var soundChannel:SoundChannel;
private var bytes:ByteArray;
private var incbytes:ByteArray;
private var mixedBytes:ByteArray;
public var pitchShiftFactor:Number;
public var position:Number;
public var AddSound:Boolean = false;
public var incremental:Number;
public var left1:Number;
public var left2:Number;
public var right1:Number;
public var right2:Number;
public var mixedBytes1:Number;
public var mixedBytes2:Number;
public function SoundFx() {
}
public function playFx(srcSound:Sound):void{
this.srcSound = srcSound;
position = 0;
var morphedSound:Sound = new Sound();
morphedSound.addEventListener(SampleDataEvent.SAMPLE_DATA, sampleDataHandler);
soundChannel = morphedSound.play();
}
public function addSound(sound1:Sound , sound2:Sound):void{
this.sound1 = sound1;
this.sound2 = sound2;
sound1.addEventListener(SampleDataEvent.SAMPLE_DATA, addSampleData);
position = 0;
soundChannel = sound1.play();
soundChannel = sound2.play();
AddSound = true;
}
private function addSampleData(event:SampleDataEvent):void{
position = 0;
var incbytes:ByteArray = new ByteArray();
position += sound1.extract(incbytes, 4096, position);
var bytes:ByteArray = new ByteArray();
position += sound2.extract(bytes, 4096, position);
event.data.writeBytes(mixBytes(bytes, incbytes));
}
private function sampleDataHandler(event:SampleDataEvent):void
{
var bytes:ByteArray = new ByteArray();
position += srcSound.extract(bytes, 4096, position);
event.data.writeBytes(editBytes(bytes));
}
private function mixBytes(bytes1:ByteArray , bytes2:ByteArray ):ByteArray{
bytes.position = 0;
incbytes.position = 0;
var returnBytes:ByteArray = new ByteArray();
while(bytes1.bytesAvailable > 0)
{
left1 = bytes1.readFloat();
left2 = bytes2.readFloat();
right1 = bytes1.readFloat();
right2 = bytes2.readFloat();
mixedBytes1 = left1 + left2;
mixedBytes2 = right1 + right1;
mixedBytes.writeFloat(mixedBytes1);
mixedBytes.writeFloat(mixedBytes2);
}
return mixedBytes;
}
private function editBytes(bytes:ByteArray):ByteArray{
//var skipCount:Number = 0;
var returnBytes:ByteArray = new ByteArray();
bytes.position = 0;
while(bytes.bytesAvailable > 0)
{
//skipCount++;
returnBytes.writeFloat(bytes.readFloat());
returnBytes.writeFloat(bytes.readFloat());
}
return returnBytes;
}
}
i think i get your idea,hope i did.
You should try to pass all of your SoundChannel classes (sound layers being added) to a SoundMixer class (there is plenty of info on this in adobe site),like a funnel,and then add a SAMPLEDATA event listener to that soundmixer, and capture the byte array of that sound mixer,it is like getting the byte array of the final mixdown...then encode those bytes to wav or mp3 .
Sorry i can not write that code down for you,i am not very good with the code yet, but im good at mixing sounds.
Here is an example form adobe site itself:
function loaded(event:Event):void
{
outputSnd.addEventListener(SampleDataEvent.SAMPLE_DATA, processSound);
outputSnd.play();
}
Useful link:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html
soundmixer class link:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/SoundMixer.html
experimenting with stratus (cirrus) internet radio i used SoundMixer.computeSpectrum() on the server side and passed that data to the client side with NetStream.send() for drawing and playback.
It worked, but was very ram hungry with client and server on one computer (and with several songs loaded and converted to raw audio data :)
so, try computeSpectrum(), it seems perfect for creating an "audio log"

Papervision3D cube face orientation

I have a question about Papervision3D, or perhaps bitmapData, I am unsure where the problem is. I have constructed a program that takes 4 banners and splits them into pieces and then applies those pieces to cubes so that I can make a banner rotator. So after I run my program I have 10 cubes with a piece of a banner on 4 faces(front, top, back, bottom) of each cube. The problem is that some of the faces are oriented incorrectly(spun 180 degrees). Is there a way in Papervision3D to spin a cube face? The other place I think the problem may be is when I create the bitmapData that will be applied to the cube faces. Is there some way to explicitly define orientation during bitmapData creation? Any help or ideas would be greatly appreciated. Thanks!
-------Edit----------
Here is the code for my CubeMaker.as class. This is the class that takes the image pieces and applies them to the cubes.
package {
import away3d.events.MaterialEvent;
import away3d.materials.BitmapMaterial;
import away3d.materials.ColorMaterial;
import away3d.materials.TransformBitmapMaterial;
import away3d.primitives.Cube;
import away3d.primitives.data.CubeMaterialsData;
import CubeEvent;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
public class CubeMaker extends Sprite {
private var _colorMaterial:ColorMaterial = new ColorMaterial(0x000000);
private var _dcRef;
private var _banners:Array;
private var _cubeArray:Array = [];
private var _cubeCoung:int = 0;
private var _numberOfPieces:int;
private var _width:Number;
private var _depth:Number;
private var _height:Number;
public function CubeMaker(banners:Array, numberOfPieces:int) {
_dcRef = DocumentClass._base;
_banners = banners;
_numberOfPieces = numberOfPieces;
}
public function makeCubes() {
//loop through the cubes
for (var i = 0; i < _numberOfPieces; i++) {
var faceBitmapArray:Array;
//fill array with four faces for current cube
faceBitmapArray = fillFace(i);
//get width and height from a piece instance
var width:Number;
var height:Number;
var tempArray:Array;
tempArray = _banners[0];
_width = tempArray[0].width;
_height = tempArray[0].height;
_depth = tempArray[0].height;
//create four materials from bitmapData
var myFront:TransformBitmapMaterial = new TransformBitmapMaterial(faceBitmapArray[0], {rotation:0} );
var myTop:TransformBitmapMaterial = new TransformBitmapMaterial(faceBitmapArray[1], {rotation:0.5} );
var myBack:TransformBitmapMaterial = new TransformBitmapMaterial(faceBitmapArray[2], {rotation:0} );
var myBottom:TransformBitmapMaterial = new TransformBitmapMaterial(faceBitmapArray[3], {rotation:0} );
//create two materians from color
var myLeft:ColorMaterial = new ColorMaterial(0x000000);
var myRight:ColorMaterial = new ColorMaterial(0x000000);
//create a CubeMatrialsData from the materials created above
var myMaterials:CubeMaterialsData = new CubeMaterialsData( { front:myFront, back:myBack, top:myTop, bottom:myBottom, left:myLeft, right:myRight} );
//listen for material change
myMaterials.addOnMaterialChange(materialChanged);
//create a new cube with the CubeMaterialsData created earlier
var _cube = new Cube({width:_width, height:_height, depth:_depth, cubeMaterials:myMaterials});
//the created cube is put into the _cubeArray
_cubeArray[i] = _cube;
if (i == (_numberOfPieces - 1)) cubesMade();
}
}
private function fillFace(i:int):Array {
var faceBitmapArray:Array = [];
for (var j = 0; j < 4; j++) {
//tempBannerArray filled with one banner in pieces
var tempBannerArray:Array = _banners[j];
//batmapData created and sized to current banner piece
var bitmapData:BitmapData = new BitmapData(tempBannerArray[i].width, tempBannerArray[i].height);
//bitmapData filled with current banner piece bitmap data
bitmapData = tempBannerArray[i].bitmapData;
bitmapData.lock();
//array is filled with bitmap data
faceBitmapArray[j] = bitmapData;
}
return faceBitmapArray;
}
private function cubesMade() {
//dispatch event to notify of cube making completion
dispatchEvent(new CubeEvent(CubeEvent.CUBES_MADE, _cubeArray.length));
}
private function materialChanged(e:MaterialEvent):void {
trace("Warning! Warning! Material changed!");
}
public function get cubes():Array {
return _cubeArray;
}
public function get cubeWidth():Number {
return _width;
}
public function get cubeHeight():Number {
return _height;
}
public function get cubeDepth():Number {
return _depth;
}
}
}
Preface: Papervision3D is close to unsupported / closed as a library, so I highly recommend using Away3D or another actionscript 3D library. Though the work done on PV3D was groundbreaking and incredible from a flash perspective so if you're looking to build a 3D experience with it, it's quite good. Just know from the get-go that future dev + support are unlikely.
Preface aside, take a look at the "Cube" primitive class in the library. You can create Materials and attach them to the MaterialsList instance that will be added to the cube instance. Assuming you're using BitmapMaterial, it has a "rotation" property, that you can set. Just determine which materials are upside-down and make their rotation = 180. Pros: quick; Cons: hard-coded.
The other option is to hit the problem on the "asset" end and flip your images. Pros: quick; Cons: non-scalable or puts the issue on the designer.
The best option, when loading the data / assets, is to have a "rotate" property that you can use to set which images should be flipped. It's a cube, so like Ender says, up is relative.

Debugging a release only flash issue

I've got an Adobe Flash 10 program that freezes in certain cases, however only when running under a release version of the flash player. With the debug version, the application works fine.
What are the best approaches to debugging such issues? I considered installing the release player on my computer and trying to set some kind of non-graphical method of output up (I guess there's some way to write a log file or similar?), however I see no way to have both the release and debug versions installed anyway :( .
EDIT: Ok I managed to replace my version of flash player with the release version, and no freeze...so what I know so far is:
Flash: Debug Release
Vista 32: works works
XP PRO 32: works* freeze
I gave them the debug players I had to test this
Hmm, seeming less and less like an error in my code and more like a bug in the player (10.0.45.2 in all cases)... At the very least id like to see the callstack at the point it freezes. Is there some way to do that without requiring them to install various bits and pieces, e.g. by letting flash write out a log.txt or something with a "trace" like function I can insert in the code in question?
EDIT2: I just gave the swf to another person with XP 32bit, same results :(
EDIT3:
Ok, through extensive use of flash.external.ExternalInterface.call("alert", "..."); I managed to find the exact line causing the problem (I also improved exception handling code so rather than freeze it told me there was an "unhandled" exception). The problem now is what on earth is flashes problem with this with the release player on some machines...
particles.push(p);
Which causes a TypeError #1034 on said platforms. Particles is a Vector.<Particle>, p is a Particle. I tested with getQualifiedClassName and got:
getQualifiedClassName(p) = ::Particle
getQualifiedClassName(particles) = __AS3__.vec::Vector.<::Particle>
Any ideas why this is a problem and what to do to make it work?
EDIT4:
Ok I seem to have solved this. The Particle class is just a simple internal class located after the package {...} in the action script file using it. I moved this into its own file (particle.as) and made it a proper public class in my package, and problem solved.
Maybe its a flash bug or maybe I missed the memo about not using internal classes in vectors or something, although if that's the case I would have expected something or other (either at compile time or with debug runtimes) to disallow it explicitly, e.g. some error on the "private var particles:Vector.<Particle>;" line. If I get a chance I guess I'll take a look at contacting the Adobe flash team concerning this or something.
Thanks for help giving debugging tips which I guess is more along the original questions lines :)
This is a long shot, but are the Particles the objects that you are clicking? If so then catching the event in the wrong phase of bubbling, and pushing event.target (assuming it to be a Particle) could cause that problem.
Whatever the problem, I have something that should help you debug. A class that creates a pseudo trace window in your SWF, much nicer than extinterfacing to javascript. I've forgotten who wrote it, but I feel like it's Senocular. I use it any time I need to get traces back from end users.
Just drop it in the default package for your project, call stage.addChild(new Output());, and then to trace call Output.trace("A message");
package {
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Stage;
import flash.display.GradientType;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.TextFieldAutoSize;
/**
* Creates a pseudo Output panel in a publish
* swf for displaying trace statements.
* For the output panel to capture trace
* statements, you must use Output.trace()
* and add an instance to the stage:
* stage.addChild(new Output());
*
*/
public class Output extends Sprite {
private var output_txt:TextField;
private var titleBar:Sprite;
private static var instance:Output;
private static var autoExpand:Boolean = false;
private static var maxLength:int = 1000;
public function Output(outputHeight:uint = 400){
if (instance && instance.parent){
instance.parent.removeChild(this);
}
instance = this;
addChild(newOutputField(outputHeight));
addChild(newTitleBar());
addEventListener(Event.ADDED, added);
addEventListener(Event.REMOVED, removed);
}
// public methods
public static function trace(str:*):void {
if (!instance) return;
instance.output_txt.appendText(str+"\n");
if (instance.output_txt.length > maxLength) {
instance.output_txt.text = instance.output_txt.text.slice(-maxLength);
}
instance.output_txt.scrollV = instance.output_txt.maxScrollV;
if (autoExpand && !instance.output_txt.visible) instance.toggleCollapse();
}
public static function clear():void {
if (!instance) return;
instance.output_txt.text = "";
}
private function newOutputField(outputHeight:uint):TextField {
output_txt = new TextField();
//output_txt.type = TextFieldType.INPUT;
output_txt.border = true;
output_txt.borderColor = 0;
output_txt.background = true;
output_txt.backgroundColor = 0xFFFFFF;
output_txt.height = outputHeight;
var format:TextFormat = output_txt.getTextFormat();
format.font = "_sans";
output_txt.setTextFormat(format);
output_txt.defaultTextFormat = format;
return output_txt;
}
private function newTitleBar():Sprite {
var barGraphics:Shape = new Shape();
barGraphics.name = "bar";
var colors:Array = new Array(0xE0E0F0, 0xB0C0D0, 0xE0E0F0);
var alphas:Array = new Array(1, 1, 1);
var ratios:Array = new Array(0, 50, 255);
var gradientMatrix:Matrix = new Matrix();
gradientMatrix.createGradientBox(18, 18, Math.PI/2, 0, 0);
barGraphics.graphics.lineStyle(0);
barGraphics.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
barGraphics.graphics.drawRect(0, 0, 18, 18);
var barLabel:TextField = new TextField();
barLabel.autoSize = TextFieldAutoSize.LEFT;
barLabel.selectable = false;
barLabel.text = "Output";
var format:TextFormat = barLabel.getTextFormat();
format.font = "_sans";
barLabel.setTextFormat(format);
titleBar = new Sprite();
titleBar.addChild(barGraphics);
titleBar.addChild(barLabel);
return titleBar;
}
// Event handlers
private function added(evt:Event):void {
stage.addEventListener(Event.RESIZE, fitToStage);
titleBar.addEventListener(MouseEvent.CLICK, toggleCollapse);
fitToStage();
toggleCollapse();
}
private function removed(evt:Event):void {
stage.removeEventListener(Event.RESIZE, fitToStage);
titleBar.removeEventListener(MouseEvent.CLICK, toggleCollapse);
}
private function toggleCollapse(evt:Event = null):void {
if (!instance) return;
output_txt.visible = !output_txt.visible;
fitToStage(evt);
}
private function fitToStage(evt:Event = null):void {
if (!stage) return;
output_txt.width = stage.stageWidth;
output_txt.y = stage.stageHeight - output_txt.height;
titleBar.y = (output_txt.visible) ? output_txt.y - titleBar.height : stage.stageHeight - titleBar.height;
titleBar.getChildByName("bar").width = stage.stageWidth;
}
}
}
Judging by when the freeze occurs, try to pinpoint some possibilities for what the offending code may be, and use De MonsterDebugger to check variables etc.
EDIT:
I'm pretty certain that the actual call stack is only available to you in the debug versions of the Flash Player / AIR. Still, it may be useful in the debug player to trace the stack from within the handler for the button to see if anything is out of place:
var err:Error = new Error(“An Error”);
trace(err.getStackTrace());
FYI the getStackTrace method is only available in the debug player, so there is no way to write it to a log.txt in production.