dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Loading swf in this very complicated script (for me lol)

Hybrid View

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    4

    Loading swf in this very complicated script (for me lol)

    Alright the defualt for this script is to join 6 images together and have them rotate as a cube. but i want to replace the images with swf's.I also want to know if its ok if the cube script is AS 3 and the swf is AS 2...

    heres the part i need to replace the images with swf :
    Code:
    var bdGames:BitmapData=new Games(230,230);
    
    var bmGames:Bitmap=new Bitmap(bdGames);
    
    var bdAnim:BitmapData=new Anim(230,230);
    
    var bmAnim:Bitmap=new Bitmap(bdAnim);
    
    var bdEas:BitmapData=new Eas(230,230);
    
    var bmEas:Bitmap=new Bitmap(bdEas);
    
    var bdBible:BitmapData=new Bible(230,230);
    
    var bmBible:Bitmap=new Bitmap(bdBible);
    
    var bdOop:BitmapData=new Oop(230,230);
    
    var bmOop:Bitmap=new Bitmap(bdOop);
    
    var bdPatterns:BitmapData=new Patterns(230,230);
    
    var bmPatterns:Bitmap=new Bitmap(bdPatterns);

    heres the full code code:

    Code:
    /*
    ActionScript 3 Tutorials by Barbara Kaskosz and Doug Ensley.
    
    www.flashandmath.com
    
    Last modified: January 29, 2008. 
    
    */
    
    /*
    The basic 3D engine in this example is the same as in our tutorial
    'Simple 3D Drawing in Flash CS3', Part 1: simple3d_1.fla. We point
    out below the slight changes that we made to the engine, but mostly
    we focus our comments on the issue of handling and transforming
    bitmaps that serve here as the sides of our cube. In simple3d_1.fla,
    the sides of our cube were simple fills drawn on the fly. Here,
    we need to transform every bitmap to correspond to a given view.
    */
    
    var vertsArray:Array=[];
    
    var facesArray:Array=[];
    
    var numVertices:uint=8;
    
    var numFaces:uint=6;
    
    /*
    If you wanted to mofify the cube menu presented in this applet, you can change the size
    of the cube by changing the value of the cubeSize variable.
    Of course, you can change the bitmaps as well.
    The only condition that must be satisfied is that the size of your
    square bitmaps must be equal to cubeSize. Here, the bitmaps are 140 by 140,
    cubeSize is 140.
    */
    
    var cubeSize:Number=230;
    
    var spBoard:Sprite=new Sprite();
    
    this.addChild(spBoard);
    
    spBoard.x=30+(cubeSize+20);
    
    spBoard.y=75+(cubeSize+20);
    
    var shBack:Shape=new Shape();
    
    spBoard.addChild(shBack);
    
    drawBack();
    
    var spCube:Sprite=new Sprite();
    
    spBoard.addChild(spCube);
    
    /*
    Each bitmap is going to reside in a separte Sprite, spSide0, spSide1, and so on.
    We want them in separate containers so they can respond individually to mouse clicks.
    */
    
    var spSide0:Sprite=new Sprite();
    
    spCube.addChild(spSide0);
    
    var spSide1:Sprite=new Sprite();
    
    spCube.addChild(spSide1);
    
    var spSide2:Sprite=new Sprite();
    
    spCube.addChild(spSide2);
    
    var spSide3:Sprite=new Sprite();
    
    spCube.addChild(spSide3);
    
    var spSide4:Sprite=new Sprite();
    
    spCube.addChild(spSide4);
    
    var spSide5:Sprite=new Sprite();
    
    spCube.addChild(spSide5);
    
    /*
    We imported our bitmaps Anim, Games etc., into the Library and linked them
    to AS via the Linkage item in the Library menu. When we established each linkage,
    Flash created subclasses of the BitmapData class by the names Anim, Games etc.
    Now, we can instantiate those classes. After instantiating each BitmapData
    sublass, we pass it to the constructor of the Bitmap class. 
    See the tutorial '3D Menu, Bitmap Fills and Loading External Files'
    for a discussion of the BitmapData versus the Bitmap classes.
    Our variables bmAnim, bmEas, etc. are bitmaps that hold
    the pixel data from bdAnim, bdEas and so on.
    */
    
    var bdGames:BitmapData=new Games(230,230);
    
    var bmGames:Bitmap=new Bitmap(bdGames);
    
    var bdAnim:BitmapData=new Anim(230,230);
    
    var bmAnim:Bitmap=new Bitmap(bdAnim);
    
    var bdEas:BitmapData=new Eas(230,230);
    
    var bmEas:Bitmap=new Bitmap(bdEas);
    
    var bdBible:BitmapData=new Bible(230,230);
    
    var bmBible:Bitmap=new Bitmap(bdBible);
    
    var bdOop:BitmapData=new Oop(230,230);
    
    var bmOop:Bitmap=new Bitmap(bdOop);
    
    var bdPatterns:BitmapData=new Patterns(230,230);
    
    var bmPatterns:Bitmap=new Bitmap(bdPatterns);
    
    /*
    We add are the bitmaps to the Display List as children of each corresponding side.
    */
    
    spSide0.addChild(bmOop);
    
    spSide1.addChild(bmGames);
    
    spSide2.addChild(bmEas);
    
    spSide3.addChild(bmAnim);
    
    spSide4.addChild(bmBible);
    
    spSide5.addChild(bmPatterns);
    
    var doRotate:Boolean=false;
    
    
    
    var prevX:Number;
    
    var prevY:Number;
    
    var curTheta:Number=20;
    
    var curPhi:Number=70;
    
    //We enable each side for the DOUBLE_CLICK event.
    
    spSide0.doubleClickEnabled=true;
    
    spSide1.doubleClickEnabled=true;
    
    spSide2.doubleClickEnabled=true;
    
    spSide3.doubleClickEnabled=true;
    
    spSide4.doubleClickEnabled=true;
    
    spSide5.doubleClickEnabled=true;
    
    //We create a text field in which info about each book will be displayed.
    
    var infoBox:TextField=new TextField();
    
    this.addChild(infoBox);
    
    infoBox.x=372;
    
    infoBox.y=75;
    
    infoBox.width=255;
    
    infoBox.height=220;
    
    infoBox.multiline=true;
    
    infoBox.wordWrap=true;
    
    infoBox.mouseEnabled=false;
    
    
    function drawBack():void {
    	
    
    	
    	shBack.graphics.drawRect(-(cubeSize+20),-(cubeSize+20),2*(cubeSize+20),2*(cubeSize+20));
    	
    	shBack.graphics.endFill();
    	
    }
    
    setVertices();
    
    setFaces();
    
    renderView(curTheta,curPhi);
    
    
    function setVertices():void {
    	
    	vertsArray[0]=[cubeSize/2,-cubeSize/2,cubeSize/2];
    	
    	vertsArray[1]=[cubeSize/2,cubeSize/2,cubeSize/2];
    	
    	vertsArray[2]=[-cubeSize/2,cubeSize/2,cubeSize/2];
    	
    	vertsArray[3]=[-cubeSize/2,-cubeSize/2,cubeSize/2];
    	
    	vertsArray[4]=[cubeSize/2,-cubeSize/2,-cubeSize/2];
    	
    	vertsArray[5]=[cubeSize/2,cubeSize/2,-cubeSize/2];
    	
    	vertsArray[6]=[-cubeSize/2,cubeSize/2,-cubeSize/2];
    	
    	vertsArray[7]=[-cubeSize/2,-cubeSize/2,-cubeSize/2];
    	
    }
    
    //Each face as before is a list of vertices. We make sure that vertices in each
    //face are ordered with the clockwise orientation. We add one more element 
    //to each face: the name of the bitmap corresponding to the face.
    
    function setFaces():void {
    	
    	facesArray[0]=[0,1,5,4,bmOop];
    	
    	facesArray[1]=[1,2,6,5,bmGames];
    	
    	facesArray[2]=[2,3,7,6,bmEas];
    	
    	facesArray[3]=[3,0,4,7,bmAnim];
    	
    	facesArray[4]=[4,5,6,7,bmBible];
    	
    	facesArray[5]=[3,2,1,0,bmPatterns];
    	
    }
    
    
    function renderView(t:Number,p:Number):void {
    	
    	var i:int;
    	
    	var distArray=[];
    	
    	var dispArray=[];
    	
    	var vertsNewArray=[];
    	
    	var midPoint:Array=[];
    	
    	//We add a few local variables for convenience.
    	
    	var curv0:Array=[];
    	
    	var curv1:Array=[];
    	
    	var curv2:Array=[];
    	
    	var curv3:Array=[];
    	
    	var curNormal:Array=[];
    	
    	//The next two variables will hold the current transform matrix corresponding
    	//to each side being displayed; curImg holds the bitmap itself. ('cur' stands for 'current'.)
    	
    	var curTransMatrix:Matrix;
    	
    	var curImg:Bitmap;
    
    	var dist:Number;
    	
    	var curFace:uint;
    	
    	//We will have to make some adjustments to bitmaps if the cube is upsidedown.
    	
    	var isUpsideDown:Boolean;
    	
    	t=t*Math.PI/180;
    	
    	p=p*Math.PI/180;
    	
    	bmGames.visible=false;
    	
    	bmAnim.visible=false;
    	
    	bmBible.visible=false;
    	
    	bmEas.visible=false;
    	
    	bmOop.visible=false;
    	
    	bmPatterns.visible=false;
    	
    	for(i=0;i<numVertices;i++){
    		
    		vertsNewArray[i]=pointNewView(vertsArray[i],t,p); 
    		
    	}
    	
    	curNormal=pointNewView([0,0,140],t,p);
    	
    	if(curNormal[2]>=0){
    		
    		isUpsideDown=false;
    		
    	} else {
    		
    		isUpsideDown=true;
    	}
    	
    
    	for(i=0;i<numFaces;i++){
    		
    		midPoint[0]=(vertsNewArray[facesArray[i][0]][0]+vertsNewArray[facesArray[i][1]][0]+vertsNewArray[facesArray[i][2]][0]+vertsNewArray[facesArray[i][3]][0])/4;
    		
    		midPoint[1]=(vertsNewArray[facesArray[i][0]][1]+vertsNewArray[facesArray[i][1]][1]+vertsNewArray[facesArray[i][2]][1]+vertsNewArray[facesArray[i][3]][1])/4;
    		
    		midPoint[2]=(vertsNewArray[facesArray[i][0]][2]+vertsNewArray[facesArray[i][1]][2]+vertsNewArray[facesArray[i][2]][2]+vertsNewArray[facesArray[i][3]][2])/4;
    		
    		dist=-midPoint[0];
    		
    		distArray[i]=[dist,i];
    		
    	}
    	
    	distArray.sort(byDist);
    	
    	/*
    	dispArray is obtained by projecting vertices from the 3D xyz-space onto the yz-plane,
    	which becomes Flash's xy-space. Our method of projection has changed: we do not have
    	any distortion for perspective. Since our distortion in simple3d_1.fla was very small
    	to begin with, lack of distortion makes little difference to the viewer. It makes all
    	the difference from the point of view of processing our bitmaps. Without perspective,
    	the projection of each wall is a parallelogram. A rectangular bitmap can be made to fit
    	into a parallelogram via available methods of scaling, skewing, and rotation.
    	*/
    	
    	for(i=0;i<numVertices;i++){
    			
    		dispArray[i]=[vertsNewArray[i][1],-vertsNewArray[i][2]];
    		
    	}
    	
    	for(i=3;i<numFaces;i++){
    		
    		//Only the three sides closest to the screen are visible.
    		
    		curFace=distArray[i][1];
    		
    		curv0=[dispArray[facesArray[curFace][0]][0],dispArray[facesArray[curFace][0]][1]];
    		
    		curv1=[dispArray[facesArray[curFace][1]][0],dispArray[facesArray[curFace][1]][1]];
    		
    		curv2=[dispArray[facesArray[curFace][2]][0],dispArray[facesArray[curFace][2]][1]];
    		
    		curv3=[dispArray[facesArray[curFace][3]][0],dispArray[facesArray[curFace][3]][1]];
    		
    		curImg=facesArray[curFace][4];
    		
    		curImg.visible=true;
    		
    		//We display the faces in order of their depth using setChildIndex method.
    		
    		spCube.setChildIndex(this["spSide"+String(curFace)],spCube.numChildren-1);
    		
    		//Now we are going to transform each bitmap to fit into the corresponding
    		//parallelogram. We do it by assigning the right transform matrix
    		//to curImg.tranform.matrix property. The transform matrix is
    		//calculated differently for the vertical sides (faces 0,1,2,3)
    		//and differently for the top and the bottom (faces 4,5).
    		
    		
    		if(curFace==0 || curFace==1 || curFace==2 || curFace==3){
    			
    		if(isUpsideDown==false){
    			
    		//Calculating the transform matrix is done based on the vertices v0,v1,v2,v3
    		//of the parallelogram and it is done by calcMatrixForSides function defined later.
    		
    		curTransMatrix=calcMatrixForSides(curv0,curv1,curv2,curv3);
    		
    		curImg.transform.matrix=curTransMatrix;
    		
    		} else {
    			
    		curTransMatrix=calcMatrixForSides(curv2,curv3,curv0,curv1);
    		
    		curImg.transform.matrix=curTransMatrix;
    			
    		}
    		
    		}
    		
    		if(curFace==5 || curFace==4){
    			
    		    curTransMatrix=calcMatrixForTops(curv0,curv1,curv2,curv3);
    		
    		    curImg.transform.matrix=curTransMatrix;
    		
    		}
    		
    	}
    	
    }
    
    function byDist(v:Array,w:Array):Number {
    	
    	if (v[0]>w[0]){
    		
    		return -1;
    		
    	  } else if (v[0]<w[0]){
    		
    		return 1;
    	
    	   } else {
    		
    		return 0;
    	  }
    	  
      }
    
    
    function pointNewView(v:Array,theta:Number,phi:Number):Array {
    	
    	var newCoords:Array=[];
    	
    	newCoords[0]=v[0]*Math.cos(theta)*Math.sin(phi)+v[1]*Math.sin(theta)*Math.sin(phi)+v[2]*Math.cos(phi);
    	
    	newCoords[1]=-v[0]*Math.sin(theta)+v[1]*Math.cos(theta);
    	
    	newCoords[2]=-v[0]*Math.cos(theta)*Math.cos(phi)-v[1]*Math.sin(theta)*Math.cos(phi)+v[2]*Math.sin(phi);
    	
    	return newCoords;
    	
    	
    }
    
    /*
    The next two functions calculate the transform matrix for the vertical
    sides and then for the top and the bottom. The calculations that make each bitmap
    fit into the corresponding parallelogram are a bit different in each case.
    For the vertical sides, parallelograms are scaled in x and y, and skewed 
    in the x direction (up or down). There is no rotation as our vertical axes
    stays vertical for every view. In the case of the top and the bottom,
    we have a possible rotation and a y skew (as well as x and y scaling).
    */
    
    function calcMatrixForSides(v0:Array,v1:Array,v2:Array,v3:Array):Matrix {
    	
    	var curXSize:Number;
    	
    	var curYSize:Number;
    	
    	var curMatrix:Matrix;
    	
    	var skewMatrix:Matrix;
    	
    	var transMatrix:Matrix;
    	
    	var curLen:Number;
    	
    	var v:Array=calcDiff(v1,v0);
    	
    	var w:Array=calcDiff(v3,v0);
    	
    	var ang:Number=calcAngle(v,w);
    	
    	if(isNaN(ang)){
    		
    		curMatrix=new Matrix(0,0,0,0,0,0);
    		
    		return curMatrix;
    	}
    	
    	curLen=calcMag(calcProj(v,[-w[1],w[0]])); 
    	
    	curXSize=curLen;
    	
    	curYSize=calcMag(w);
    	
    	curMatrix=new Matrix(curXSize/cubeSize,0,0,curYSize/cubeSize,0,0);
    	
    	skewMatrix=new Matrix(1,Math.tan(Math.PI/2-ang),0,1,0,0);
    	
    	curMatrix.concat(skewMatrix);
    	
    	transMatrix=new Matrix(1,0,0,1,v0[0],v0[1]);
    	
    	curMatrix.concat(transMatrix);
    	
    	return curMatrix;
    	
    }
    
    function calcMatrixForTops(v0:Array,v1:Array,v2:Array,v3:Array):Matrix {
    	
    	var curXSize:Number;
    	
    	var curYSize:Number;
    	
    	var curMatrix:Matrix;
    	
    	var skewMatrix:Matrix;
    	
    	var rotMatrix:Matrix;
    	
    	var transMatrix:Matrix;
    	
    	var curLen:Number;
    	
    thread to long.

  2. #2
    Junior Member
    Join Date
    May 2008
    Posts
    4
    thanks for the help guys

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    4
    please help!!!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center