The problem iam facing is that i trying to put my puzzle pieces into a 3D object container(since i searched for the source and many suggest to use displayobject3d to do it). My puzzles piecesare Sprite data type, but i am having problem to put all my puzzle pieces into the displayobject3d container.
Below is my code. Some of you might see this code before, because i get it from the book titled "G._Rosenzweig_ActionScript_3.0_Game_Programming_University-2nd_Ed". i have added a few addition which is the program allow player to choose an image from his local disk, and enter the number of dimension of the puzzle game. But my only concern is to convert it into a 3d sliding puzzle game.
Hope anyone help is kind to help me on this.
package {
import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.geom.*;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.display.BitmapData;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.text.*;
import org.papervision3d.materials.ColorMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.view.BasicView;
import org.papervision3d.materials.BitmapFileMaterial;
import org.papervision3d.events.FileLoadEvent;
import org.papervision3d.materials.BitmapAssetMaterial;
import org.papervision3d.materials.MovieMaterial;
import org.papervision3d.cameras.Camera3D;
import flash.media.Camera;
import away3d.containers.View3D;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.scenes.Scene3D;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.materials.BitmapMaterial;
public class SlidingPuzzle extends MovieClip {
// space between pieces and offset
static const pieceSpace:Number = 2;
static const horizOffset:Number = 50;
static const vertOffset:Number = 50;
// number of pieces
private var numPiecesHoriz:int;
private var numPiecesVert:int;
// random shuffle steps
static const numShuffle:int = 200;
// animation steps and time
static const slideSteps:int = 10;
static const slideTime:int = 250;
// size of pieces
private var pieceWidth:Number;
private var pieceHeight:Number;
// game pieces
private var puzzleObjects:Array;
private var selectedImage:String;
// tracking moves
private var blankPoint:Point;
private var slidingPiece:Object;
private var slideDirection:Point;
private var slideAnimation:Timer;
private var getRowNumbers:TextField;
private var getColNumbers:TextField;
private var startTime:uint;
private var playTime:uint;
private var totalTimeField:TextField;
private var plane1:Plane;
private var planeContainer:DisplayObject3D = new DisplayObject3D;
private var camera:Camera3D = new Camera3D();
private var view:Viewport3D = new Viewport3D();
private var scene:Scene3D = new Scene3D();
private var render:BasicRenderEngine = new BasicRenderEngine();
var planeArray:Array = new Array();
var xDist:Number = mouseX - stage.stageWidth * 0.5;
var yDist:Number = mouseY - stage.stageHeight * 0.5;
private var easeOut:Number = 0.3;
private var reachX:Number = 0.5;
private var reachY:Number = 0.5;
private var reachZ:Number = 0.05;
public function startSlidingPuzzle() {
//stage.scaleMode = StageScaleMode.NO_SCALE;
//stage.align = StageAlign.TOP_LEFT;
addChild(view);
// blank spot is the bottom right
//blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);
blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);
//getDimension();
loadBitmap();
setGameTimeField();
//scene.addChild(plane1);
//this.addChild(view);
//render.renderScene(scene, camera, view);
//this.addEventListener(Event.ENTER_FRAME, render);
}
public function setGameTimeField():void{
totalTimeField = new TextField();
totalTimeField.x = 550;
startTime = getTimer();
playTime = 0;
addEventListener(Event.ENTER_FRAME, displayTime);
addChild(totalTimeField);
}
public function displayTime(event:Event){
playTime = getTimer()-startTime;
totalTimeField.text = "Time: " + clockTime(playTime);
}
public function clockTime(ms:int){
var second:int = Math.floor(ms/1000);
var minute:int = Math.floor(second/60);
var timeString:String = minute + ":" +String(second + 100).substr(1, 2);
return timeString;
}
// get the bitmap from an external source
private var loader:FileReference;
public function loadBitmap() {
loader = new FileReference();
loader.addEventListener(Event.SELECT, selectHandler);
var fileFilter:FileFilter = new FileFilter("Image(*.jpg,*.png,*.gif,*.bmp)", "*.jpg;*.png;*.gif;*.bmp");
loader.browse([fileFilter]);
}
private function selectHandler(event:Event):void{
loader.removeEventListener(Event.SELECT, selectHandler);
//setDisplayState(PROGRESS);
loader.addEventListener(Event.COMPLETE, loadingDone);
loader.load();
}
// bitmap done loading
public function loadingDone(event:Event):void {
loader.removeEventListener(Event.COMPLETE, loadingDone);
var fileload:Loader = new Loader();
fileload.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadBytesHandler);
fileload.loadBytes(loader.data);
}
//close loadind event and cut bitmap into pieces
public function LoadBytesHandler(event:Event):void{
var loadInfo:LoaderInfo = (event.target as LoaderInfo);
var image:Bitmap = loadInfo.content as Bitmap;
//var i:BitmapData = loadInfo.content as BitmapData;
//var bitmapMate:BitmapMaterial = new BitmapMaterial(image.bitmapData);
loadInfo.removeEventListener(Event.COMPLETE, LoadBytesHandler);
//bitmapMate.doubleSided = true;
//plane1 = new Plane(bitmapMate, 480, 320);
//scene.addChild(plane1);
//this.addChild(view);
//render.renderScene(scene, camera, view);
pieceWidth = image.width/numPiecesHoriz;
pieceHeight = image.height/numPiecesVert;
// cut into puzzle pieces
makePuzzlePieces(image.bitmapData);
// shuffle them
shufflePuzzlePieces();
}
// cut bitmap into pieces
public function makePuzzlePieces(bitmapData:BitmapData) {
puzzleObjects = new Array();
for(var x:uint=0;x<numPiecesHoriz;x++) {
planeArray[x] = new Array();
for (var y:uint=0;y<numPiecesVert;y++) {
// skip blank spot
if (blankPoint.equals(new Point(x,y))) continue;
// create new puzzle piece bitmap and sprite
var newPuzzlePieceBitmap:Bitmap = new Bitmap(new BitmapData(pieceWidth,pieceHeight));
newPuzzlePieceBitmap.bitmapData.copyPixels(bitmapData,new Rectangle(x*pieceWidth,y*pieceHeight,pieceWidth,pieceHeight),new Point(0,0));
var newPuzzlePiece:Sprite = new Sprite();
newPuzzlePiece.addChild(newPuzzlePieceBitmap);
addChild(newPuzzlePiece);
//var b:BitmapData = new BitmapData();
//b.draw(newPuzzlePiece);
//var bitmapMate:BitmapMaterial = new BitmapMaterial(B)/>;
planeContainer.addChild(newPuzzlePiece);
//addChild(planeContainer);
// set location
newPuzzlePiece.x = x*(pieceWidth+pieceSpace) + horizOffset;
newPuzzlePiece.y = y*(pieceHeight+pieceSpace) + vertOffset;
//
//
//plane1 = new Plane(bitmapMate, 350, 350);
//planeArray[x][y] = new Plane(bitmapMate, 400, 400);
//planeContainer.addChild(planeArray[x][y]);
//planeContainer.x= 400;
//planeContainer.y=400;
//pcContainer.addChild(planeContainer);
//scene.addChild(pcContainer);
// create object to store in array
var newPuzzleObject:Object = new Object();
newPuzzleObject.currentLoc = new Point(x,y);
newPuzzleObject.homeLoc = new Point(x,y);
newPuzzleObject.piece = newPuzzlePiece;
newPuzzlePiece.addEventListener(MouseEvent.CLICK,clickPuzzlePiece);
puzzleObjects.push(newPuzzleObject);
}
}
scene.addChild(planeContainer);
//for(var i:uint = 0; i<puzzleObjects; i++){
//planeArray[i] = new Array();
//for(var j:uint = 0; j<puzzleObjects; j++){
//plane1 = new Plane();
//planes.x = 50 * i + 5;
//planes.y = -50 *j ;
//array[i][j] = planes;
//container.addChild(array[i][j]);
//}
//}
//var count:int = 0;
// var planeArray:Array = new Array();
//pcContainer.addChild(planeContainer);
// for(var i:uint=0;i<planeArray.length;i++) {
// planeArray[i] = new Array();
// for(var j:uint=0;j<planeArray.length;j++){
// //planeArray[i][j] = new Plane();
// planeContainer.addChild(planeArray[i][j]);
// }
//// count++;
// }
// trace(count);
//plane1 = new Plane();
//pcContainer.addChild(planeContainer);
//scene.addChild(pcContainer);
//this.addChild(view);
//scene.addChild(plane1);
//camera.zoom = 200;
//addEventListener(MouseEvent.MOUSE_OVER, renderGameScene);
//addEventListener(Event.ENTER_FRAME, loop);
render.renderScene(scene, camera, view);
}
public function renderGameScene(e:MouseEvent){
camera.x += (xDist - camera.x * reachX) * easeOut;
camera.y += (yDist- camera.y * reachY) * easeOut;
render.renderScene(scene, camera, view);
}
// make a number of random moves
public function shufflePuzzlePieces() {
for(var i:int=0;i<numShuffle;i++) {
shuffleRandom();
}
}
// random move
public function shuffleRandom() {
// loop to find valid moves
var validPuzzleObjects:Array = new Array();
for(var i:uint=0;i<puzzleObjects.length;i++) {
if (validMove(puzzleObjects[i]) != "none") {
validPuzzleObjects.push(puzzleObjects[i]);
}
}
// pick a random move
var pick:uint = Math.floor(Math.random()*validPuzzleObjects.length);
movePiece(validPuzzleObjects[pick],false);
}
public function validMove(puzzleObject:Object): String {
// is the blank spot above
if ((puzzleObject.currentLoc.x == blankPoint.x) &&
(puzzleObject.currentLoc.y == blankPoint.y+1)) {
return "up";
}
// is the blank spot below
if ((puzzleObject.currentLoc.x == blankPoint.x) &&
(puzzleObject.currentLoc.y == blankPoint.y-1)) {
return "down";
}
// is the blank to the left
if ((puzzleObject.currentLoc.y == blankPoint.y) &&
(puzzleObject.currentLoc.x == blankPoint.x+1)) {
return "left";
}
// is the blank to the right
if ((puzzleObject.currentLoc.y == blankPoint.y) &&
(puzzleObject.currentLoc.x == blankPoint.x-1)) {
return "right";
}
// no valid moves
return "none";
}
// puzzle piece clicked
public function clickPuzzlePiece(event:MouseEvent) {
// find piece clicked and move it
for(var i:int=0;i<puzzleObjects.length;i++) {
if (puzzleObjects[i].piece == event.currentTarget) {
movePiece(puzzleObjects[i],true);
break;
}
}
}
// move a piece into the blank space
public function movePiece(puzzleObject:Object, slideEffect:Boolean) {
// get direction of blank space
switch (validMove(puzzleObject)) {
case "up":
movePieceInDirection(puzzleObject,0,-1,slideEffect);
break;
case "down":
movePieceInDirection(puzzleObject,0,1,slideEffect);
break;
case "left":
movePieceInDirection(puzzleObject,-1,0,slideEffect);
break;
case "right":
movePieceInDirection(puzzleObject,1,0,slideEffect);
break;
}
}
// move the piece into the blank spot
public function movePieceInDirection(puzzleObject:Object, dx,dy:int, slideEffect:Boolean) {
puzzleObject.currentLoc.x += dx;
puzzleObject.currentLoc.y += dy;
blankPoint.x -= dx;
blankPoint.y -= dy;
// animate or not
if (slideEffect) {
// start animation
startSlide(puzzleObject,dx*(pieceWidth+pieceSpace),dy*(pieceHeight+pieceSpace));
} else {
// no animation, just move
puzzleObject.piece.x = puzzleObject.currentLoc.x*(pieceWidth+pieceSpace) + horizOffset;
puzzleObject.piece.y = puzzleObject.currentLoc.y*(pieceHeight+pieceSpace) + vertOffset;
}
}
// set up a slide
public function startSlide(puzzleObject:Object, dx, dy:Number) {
if (slideAnimation != null) slideDone(null);
slidingPiece = puzzleObject;
slideDirection = new Point(dx,dy);
slideAnimation = new Timer(slideTime/slideSteps,slideSteps);
slideAnimation.addEventListener(TimerEvent.TIMER,slidePiece);
slideAnimation.addEventListener(TimerEvent.TIMER_COMPLETE,slideDone);
slideAnimation.start();
}
// move one step in slide
public function slidePiece(event:Event) {
slidingPiece.piece.x += slideDirection.x/slideSteps;
slidingPiece.piece.y += slideDirection.y/slideSteps;
}
// complete slide
public function slideDone(event:Event) {
slidingPiece.piece.x = slidingPiece.currentLoc.x*(pieceWidth+pieceSpace) + horizOffset;
slidingPiece.piece.y = slidingPiece.currentLoc.y*(pieceHeight+pieceSpace) + vertOffset;
slideAnimation.stop();
slideAnimation = null;
// check to see if puzzle is complete now
if (puzzleComplete()) {
clearPuzzle();
MovieClip(root).gotoAndStop("gameover");
}
}
// check to see if all pieces are in place
public function puzzleComplete():Boolean {
for(var i:int=0;i<puzzleObjects.length;i++) {
if (!puzzleObjects[i].currentLoc.equals(puzzleObjects[i].homeLoc)) {
return false;
}
}
return true;
}
// remove all puzzle pieces
public function clearPuzzle() {
for (var i in puzzleObjects) {
puzzleObjects[i].piece.removeEventListener(MouseEvent.CLICK,clickPuzzlePiece);
removeChild(puzzleObjects[i].piece);
}
puzzleObjects = null;
}
}
}
Well, it's kinda messy and i apologize for that.... it is not complete yet...

New Topic/Question
Reply



MultiQuote


|