Saving an Away3D mesh to Wavefront .obj - actionscript-3

I'm trying to export some meshes generated in Away3D (with Path Extrude) to an .obj file that I can use in Three.js. I've managed to export the geometry but can figure out the UV mapping. Here's what I got so far:
private static function getObjFile(geometry:Geometry):String {
var objFile:String = "" ;
var i:int = 0;
var pri:int = 0;
var sg:SubGeometry;
var b:uint = 0;
for each(sg in geometry.subGeometries){
objFile += "\no";
i = 0;
for each(var v:Number in sg.vertexData){
if(i%3 == 0) objFile += "\nv";
i++;
objFile += " " + setPrecision(v, 1000);
}
i=0;
for each(var v:Number in sg.UVData){
if(i%2 == 0) objFile += "\nvt";
i++;
objFile += " " + setPrecision(v, 1000);
}
b = 0;
for each(var ind:uint in sg.indexData){
if(b%3 == 0) objFile += "\nf";
b++;
objFile += " " + String(ind + 1 + pri)+"/"+String(ind + 1 + pri);
}
b = 0;
for each(var n:Number in sg.vertexNormalData) {
if (b % 3 == 0) objFile += "\nvn";
objFile += " " + setPrecision(n, 1000);
b++;
}
pri = i /3;
}
return objFile;
}
What am I doing wrong?
I need the geometry and the UVs... so far if I keep only the first two loops I get valid obj with geometry and no UVs. If add the UV loop... the UV buffer is invalid for some reason.

Try out this:
private static function getObjFile(geometry:Geometry):String {
var objFile:String = "" ;
var i:int = 0;
var pri:int = 0;
var sg:SubGeometry;
var b:uint = 0;
for each(sg in geometry.subGeometries){
objFile += "\no";
i = 0;
for each(var v:Number in sg.vertexData){
if(i%3 == 0) objFile += "\nv";
i++;
objFile += " " + setPrecision(v, 1000);
}
i=0;
for each(v in sg.vertexNormalData){
if(i%3 == 0) objFile += "\nvn";
i++;
objFile += " " + setPrecision(v, 1000);
}
i=0;
for each(v in sg.UVData){
if(i%2 == 0) objFile += "\nvt";
i++;
objFile += " " + setPrecision(v, 1000);
}
b = 0;
for each(var ind:uint in sg.indexData){
if(b%3 == 0) objFile += "\nf";
b++;
var indexString:String = String(ind + 1 + pri);
objFile += " " + indexString + '/' + indexString + '/' + indexString;
}
pri = i /3;
}
return objFile;
}

Related

I need to slow down my code to see some action on screen

I am creating a slot machine and so far it looks like it is working but the for loop moves so quickly I cannot see the images being replaced.
Any ideas on how can I slow down the for loop or any other way to see the images changing on the screen?
function myFunction() {
var i;
var dicepicture;
var dice = Math.floor(Math.random() * 1000) + 1;
for (i = 0; i < dice; i++) {
var k;
for (k = 1; k < 7; k++) {
document.getElementById("first-picture" + k).src =
'images/empire.jpg';
}
var l;
for (l = 1; l < 7; l++) {
document.getElementById("first-picture" + l).src =
'images/black.jpg';
}
var j;
for (j = 1; j < 7; j++) {
var dicepicture = Math.floor(Math.random() * 11) + 1;
document.getElementById("first-picture" + j).src =
'images/image' + dicepicture + '.jpg';
document.getElementById("vari1").innerHTML = "Value of i is:
" + i;
document.getElementById("vari2").innerHTML = "Value of Dice
is: " + dice;
if (j == 1) {
document.getElementById("first-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 2) {
document.getElementById("second-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 3) {
document.getElementById("third-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 4) {
document.getElementById("forth-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 5) {
document.getElementById("fifth-pic-code").innerHTML =
"Code is: " + dicepicture;
} else if (j == 6) {
document.getElementById("sixth-pic-code").innerHTML
= "Code is: " + dicepicture;
}
}
}
document.getElementById("vari3").innerHTML = "Value of Dice is: "
+ dice
}
USE
setTimeOut(<timeout_value>) //to delay

collision detection on rectangle and ball

i am build ballz game in action script3.
i am facing a problem in corner of rectangle when hit ball it cannot determine ball next position left,or bottom.
this is my code plz help me.
this is the collision function that check direction of ball
`if (isColliding)
{
var brick:Rectangle = hitBrick.getBounds(this);
var bl:int = ba.x - (ball.width / 2);
var br:int = ba.x + (ball.width / 2);
var bt:int = ba.y - (ball.height / 2);
var bb:int = ba.y + (ball.height / 2);
var hl:int = pt.x - (hitBrick.width / 2);
var hr:int = pt.x + (hitBrick.width / 2);
var ht:int = pt.y - (hitBrick.height / 2);
var hb:int = pt.y + (hitBrick.height / 2);
if (hb <= bt)
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - " + "Bottom.");
_totalScore++;
ball._orignalYSpeed = -ball.yspeed;
ball.yspeed = -ball.yspeed + 5;
ball._stopBall = true;
ball._bottomOne = true;
ball.updateCounterInBricks(hitBrick);
return;
}
if ((hl >= bl))
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - Left");
_hitOnWall = true;
ball._orignalXSpeed = -ball.xspeed;
var xSp:Number = ball.xspeed = -ball.xspeed - 5;
ball._left = true;
ball.updateCounterInBricks(hitBrick);
_totalScore++;
return;
}
if(hb<=bb)
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - " + "Bottom.");
_totalScore++;
ball._orignalYSpeed = -ball.yspeed;
ball.yspeed = -ball.yspeed + 5;
ball._stopBall = true;
ball._bottomOne = true;
ball.updateCounterInBricks(hitBrick);
return;
}
if ((hr <= br))
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - Rigth");
_hitOnWall = true;
ball._orignalXSpeed = -ball.xspeed;
var xSp:Number = ball.xspeed = -ball.xspeed + 5;
ball._left = true;
ball._stopBall = true;
ball.updateCounterInBricks(hitBrick);
_totalScore++;
return;
}
if (ht >= bt)
{
ConsoleUtil.consoleIt("TableScreen.as :: chkColision - " + "Top");
_totalScore++;
ball._orignalYSpeed = -ball.yspeed;
ball.yspeed = -ball.yspeed - 5;
ball._topOne = true;
ball._stopBall = true;
ball.updateCounterInBricks(hitBrick);
return;
}
}`

Why my app is different when i publish it and run it on android device?

I build a platform game using adobe cs5.5 and written with as3.
When i test my app on pc the game is running perfect but when i
publish it and run it to my android device i have some gravity issues.
Sometimes when i land on ground all the stage start to tremble.
The strange is, in the same level in the same ground sometimes the stage start
tremble and sometimes not.
Is that a problem in my code or something else?
I use 800w x 480h stage.
i publish using GPU mode.
All my ground inside i draw it with shape.
var xvel = 0;
var yvel = 0;
var GRA = 1;
var FRICT = 0.85;
var downarr = new Array(-12,0,12);
var vertarr = new Array(-20,-30);
var uparr = new Array(-3,0,3);
this.addEventListener(Event.ENTER_FRAME,FramesEvents);
function windowcamera()
{
var newx = - savvito.x + 350;
var distx = newx - bg.x;
bg.x += distx;
var newy = - savvito.y + 370;
var disty = newy - bg.y;
bg.y += disty;
}
function phys()
{
savvito.x += xvel;
savvito.y += yvel;
xvel *= FRICT;
yvel += GRA;
if (yvel > 8)
{
yvel = 8;
}
if (yvel > 2)
{
onground = false;
}
savvito.virtx = savvito.x + bg.x;
savvito.virty = savvito.y + bg.y;
downPush(savvito);
leftPush(savvito);
rightPush(savvito);
upPushCloud(savvito);
upPush(savvito);
}
function runJump()
{
onground = false;
yvel = - 12;
}
function downPush(d)
{
d.virtx = d.x + bg.x;
d.virty = d.y + bg.y;
for (var i = 0; i < uparr.length; i++)
{
var num = uparr[i];
var tx = d.virtx + num;
var ty = d.virty - 40;
while (bg.ground.hitTestPoint(tx , ty , true))
{
d.virty++;
ty++;
d.y++;
yvel = 1;
}
for (var ie=0; ie<boxingArr.length; ie++)
{
var e = boxingArr[ie];
if (e.liftthisbox == 0)
{
while (e.hitTestPoint(tx , ty , true))
{
e.y--;
e.yvel = 0;
}
}
}
}
}
function rightPush(d)
{
d.virtx = d.x + bg.x + xvel;
d.virty = d.y + bg.y;
for (var i = 0; i < vertarr.length; i++)
{
var num = vertarr[i];
var tx = d.virtx + 15;
var ty = d.virty + num;
while (bg.ground.hitTestPoint(tx , ty , true))
{
d.virtx--;
tx--;
d.x--;
xvel = 0;
}
for (var ie=0; ie<boxingArr.length; ie++)
{
var e = boxingArr[ie];
if (e.liftthisbox == 0)
{
while (e.hitTestPoint(tx , ty , true))
{
d.virtx--;
tx--;
d.x--;
xvel = 0;
}
}
}
}
}
function leftPush(d)
{
d.virtx = d.x + bg.x + xvel;
d.virty = d.y + bg.y;
for (var i = 0; i < vertarr.length; i++)
{
var num = vertarr[i];
var tx = d.virtx - 15;
var ty = d.virty + num;
while (bg.ground.hitTestPoint(tx , ty , true))
{
d.virtx++;
tx++;
d.x++;
xvel = 0;
}
for (var ie=0; ie<boxingArr.length; ie++)
{
var e = boxingArr[ie];
if (e.liftthisbox == 0)
{
while (e.hitTestPoint(tx , ty , true))
{
d.virtx++;
tx++;
d.x++;
xvel = 0;
}
}
}
}
}
function upPushCloud(d)
{
if (yvel > 0)
{
d.virtx = d.x + bg.x;
d.virty = d.y + bg.y;
for (var i = 0; i < downarr.length; i++)
{
var num = downarr[i];
var tx = d.virtx + num;
var ty = d.virty;
while (bg.cloudbg.hitTestPoint(tx , ty , true))
{
onground = true;
jdown = false;
d.virty--;
ty--;
d.y--;
yvel = 0;
}
}
}
}
function upPush(d)
{
d.virtx = d.x + bg.x;
d.virty = d.y + bg.y;
for (var i = 0; i < downarr.length; i++)
{
var num = downarr[i];
var tx = d.virtx + num;
var ty = d.virty;
while (bg.ground.hitTestPoint(tx , ty , true))
{
onground = true;
jdown = false;
d.virty--;
ty--;
d.y--;
yvel = 0;
}
for (var ie=0; ie<boxingArr.length; ie++)
{
var e = boxingArr[ie];
while (e.hitTestPoint(tx , ty , true))
{
onground = true;
jdown = false;
d.virty--;
ty--;
d.y--;
yvel = 0;
}
}
}
}
function FramesEvents(e:Event)
{
phys();
controls();
windowcamera();
}

what and where is the issue in Base64bitEncoder.decodeToByteArray

Now, I do Flash Actionscript 3.0 development, I have a Base64bitEncoder class with the encodeByteArray(...) and decodeToByteArray(...) function.
but when I write the below testing code, but the byteArray.length at before Encoding and after decoding are different, could you help to see see where I wrong?
Test Sample Code as below:
var _loc_2:XML = new XML("<formXML>aaaaa</formXML>");;
var _loc_3:ByteArray;
_loc_3 = new ByteArray();
//_loc_3.endian = Endian.LITTLE_ENDIAN;
_loc_3.writeUTFBytes(_loc_2);
trace("Before Encoding Length:" + _loc_3.length);
var baseString:String = Base64bitEncoder.encodeByteArray(_loc_3);
trace(baseString);
var strArray:ByteArray = Base64bitEncoder.decodeToByteArray(baseString);
trace("After Decoding length:" + strArray.length);
The Output log as below:
Before Encoding Length:5
GFhAGFhA
After Decoding length:**6**
Why the after is 6, where I do wrong?
Base64bitEncoder source code as below:
package
{
import flash.utils.*;
public class Base64bitEncoder extends Object
{
private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
private static const BASE64_URL_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_!";
public function Base64bitEncoder()
{
return;
}// end function
public static function encode(param1:String, param2:Boolean = false) : String
{
var _loc_3:ByteArray;
_loc_3 = new ByteArray();
_loc_3.writeUTFBytes(param1);
return encodeByteArray(_loc_3, param2);
}// end function
public static function decodeToByteArray(param1:String, param2:Boolean = false) : ByteArray
{
var _loc_3:ByteArray;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = new ByteArray();
_loc_4 = new Array(4);
_loc_5 = new Array(3);
_loc_6 = 0;
while (_loc_6 < param1.length)
{
// label
_loc_7 = 0;
while (_loc_7++ < 4 && _loc_6 + _loc_7 < param1.length)
{
// label
_loc_4[_loc_7] = param2 == true ? (BASE64_URL_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7))) : (BASE64_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7)));
}// end while
_loc_5[0] = (_loc_4[0] << 2) + ((_loc_4[1] & 48) >> 4);
_loc_5[1] = ((_loc_4[1] & 15) << 4) + ((_loc_4[2] & 60) >> 2);
_loc_5[2] = ((_loc_4[2] & 3) << 6) + _loc_4[3];
_loc_8 = 0;
while (_loc_8++ < _loc_5.length)
{
// label
if (_loc_4[_loc_8 + 1] == 64)
{
break;
}// end if
_loc_3.writeByte(_loc_5[_loc_8]);
}// end while
_loc_6 = _loc_6 + 4;
}// end while
_loc_3.position = 0;
return _loc_3;
}// end function
public static function encodeByteArray(param1:ByteArray, param2:Boolean = false) : String
{
var _loc_3:String;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = "";
_loc_5 = new Array(4);
param1.position = 0;
while (param1.bytesAvailable > 0)
{
// label
_loc_4 = [];
_loc_6 = 0;
while (_loc_6++ < 3 && param1.bytesAvailable > 0)
{
// label
_loc_4[_loc_6] = param1.readUnsignedByte();
}// end while
_loc_5[0] = (_loc_4[0] & 252) >> 2;
_loc_5[1] = (_loc_4[0] & 3) << 4 | _loc_4[1] >> 4;
_loc_5[2] = (_loc_4[1] & 15) << 2 | _loc_4[2] >> 6;
_loc_5[3] = _loc_4[2] & 63;
_loc_7 = _loc_4.length;
while (_loc_7++ < 3)
{
// label
_loc_5[_loc_7 + 1] = 64;
}// end while
_loc_8 = 0;
while (_loc_8++ < _loc_5.length)
{
// label
_loc_3 = _loc_3 + (param2 == true ? (BASE64_URL_CHARS.charAt(_loc_5[_loc_8])) : (BASE64_CHARS.charAt(_loc_5[_loc_8])));
}// end while
}// end while
return _loc_3;
}// end function
public static function decode(param1:String, param2:Boolean = false) : String
{
var _loc_3:ByteArray;
_loc_3 = decodeToByteArray(param1, param2);
return _loc_3.readUTFBytes(_loc_3.length);
}// end function
}
}
#akmozo, today, I spend some time to study the base64 theory, I fix the issue, thanks.
update code:
public static function encodeByteArray(param1:ByteArray, param2:Boolean = false) : String
{
var _loc_3:String;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = "";
_loc_5 = new Array(4);
param1.position = 0;
while (param1.bytesAvailable > 0)
{
// label
_loc_4 = [];
_loc_6 = 0;
while (_loc_6 < 3 && param1.bytesAvailable > 0)
{
// label
_loc_4[_loc_6] = param1.readUnsignedByte();
_loc_6 = _loc_6 + 1;
}// end while
_loc_5[0] = (_loc_4[0] & 252) >> 2;
_loc_5[1] = (_loc_4[0] & 3) << 4 | _loc_4[1] >> 4;
_loc_5[2] = (_loc_4[1] & 15) << 2 | _loc_4[2] >> 6;
_loc_5[3] = _loc_4[2] & 63;
/*
trace("--------------------------------");
trace("0:" + _loc_4[0]);
trace("1:" + _loc_4[1]);
trace("3:" + _loc_4[2]);
trace("len:"+_loc_4.length);
trace("++++++++++++++++++++++++++++++++");
*/
_loc_7 = _loc_4.length;
while (_loc_7 < 3)
{
// label
_loc_5[_loc_7 + 1] = 64;
_loc_7 = _loc_7 + 1;
}// end while
_loc_8 = 0;
while (_loc_8 < _loc_5.length)
{
// label
_loc_3 = _loc_3 + (param2 == true ? (BASE64_URL_CHARS.charAt(_loc_5[_loc_8])) : (BASE64_CHARS.charAt(_loc_5[_loc_8])));
_loc_8 = _loc_8 + 1;
}// end while
}// end while
return _loc_3;
}// end function
public static function decodeToByteArray(param1:String, param2:Boolean = false) : ByteArray
{
var _loc_3:ByteArray;
var _loc_4:Array;
var _loc_5:Array;
var _loc_6:uint;
var _loc_7:uint;
var _loc_8:uint;
_loc_3 = new ByteArray();
_loc_4 = new Array(4);
_loc_5 = new Array(3);
_loc_6 = 0;
while (_loc_6 < param1.length)
{
// label
_loc_7 = 0;
while (_loc_7 < 4 && _loc_6 + _loc_7 < param1.length)
{
// label
_loc_4[_loc_7] = param2 == true ? (BASE64_URL_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7))) : (BASE64_CHARS.indexOf(param1.charAt(_loc_6 + _loc_7)));
_loc_7 = _loc_7 + 1;
}// end while
_loc_5[0] = (_loc_4[0] << 2) + ((_loc_4[1] & 48) >> 4);
_loc_5[1] = ((_loc_4[1] & 15) << 4) + ((_loc_4[2] & 60) >> 2);
_loc_5[2] = ((_loc_4[2] & 3) << 6) + _loc_4[3];
_loc_8 = 0;
while (_loc_8 < _loc_5.length)
{
// label
if (_loc_4[_loc_8 + 1] == 64)
{
break;
}// end if
_loc_3.writeByte(_loc_5[_loc_8]);
_loc_8 = _loc_8 + 1;
}// end while
_loc_6 = _loc_6 + 4;
}// end while
_loc_3.position = 0;
return _loc_3;
}// end function

Creating variable names

I'm having trouble with the following function:
private function whichLevelToLoad():void{
if(levelToLoad == "nothing"){
currentLevel = null;
}
var thisObj:Object = new Object();
if(levelBtnArray!=null){
for(var j:int=levelBtnArray.length-1;j>=0;j--) {
if(levelToLoad == String("level " + (j+1))){
thisObj["level"+(j+1)] = new ["Level"+(j+1)]();--------------------------->The Problem
thisObj["level" + (j+1)].x = 0;
thisObj["level" + (j+1)].y = 0;
addChildAt(thisObj["level" + (j+1)], 0);
currentLevel = thisObj["level" + (j+1)];
}
}
}
}
I'm trying to instatiate 75 objects by using a loop. The line would look like this,"thisObj.level1 = new Level1(); with the numbers going from 1-75. Is this possible? How
do I do it?
Try
if(levelBtnArray!=null){
var levelClass:Class;
for(var j:int=levelBtnArray.length-1;j>=0;j--) {
if(levelToLoad == String("level " + (j+1))){
levelClass = getDefinitionByName( "Level"+(j+1) ) as Class;
thisObj["level"+(j+1)] = new levelClass();
thisObj["level" + (j+1)].x = 0;
thisObj["level" + (j+1)].y = 0;
addChildAt(thisObj["level" + (j+1)], 0);
currentLevel = thisObj["level" + (j+1)];
}
}
}