Flash 3D mouse-controlled cube

Today it’s the time to publish a demo/prototype of a 3D mouse-controlled cube sent by Andre Marianiello.

It was made with code from the 3D tutorials on www.kirupa.com that I formatted into easy-to-use classes. Easy to make simple changes if that would be more agreeable to you. I will send source code if you want, but you don’t have to publish it (unless, of course, you want to).

As usual, Andre commented the code to make it more readable for us all

This is the main actionscript

import Space3D.*;
#include "customFunctions.as"
//to hold clips
this.createEmptyMovieClip("space", 1);
//center origin
space._x = Stage.width / 2;
space._y = Stage.height / 2;
//hold x,y,z values of the 8 points to make a cube
pointsArray = new Array();
pointsArray.push(new Point3D(-100, -100, -100));
pointsArray.push(new Point3D(-100, -100, 100));
pointsArray.push(new Point3D(-100, 100, -100));
pointsArray.push(new Point3D(-100, 100, 100));
pointsArray.push(new Point3D(100, -100, -100));
pointsArray.push(new Point3D(100, -100, 100));
pointsArray.push(new Point3D(100, 100, -100));
pointsArray.push(new Point3D(100, 100, 100));
//attach 8 clips to space's 0,0 (center of screen)
for (i = 0; i < 8; i++) {
	attachedObj = space.attachMovie("ball", "ball" + i, i);
}
//move camera towards you (comment out the next line if you want to be inside the cube
Camera3D.z = -200;
rotateCube = function () {
	//rotation of cube determined by _xmouse and _ymouse
	var xrot = this._ymouse / 900;
	var yrot = -this._xmouse / 900;
	//rotate all the points
	Universe3D.rotatePointArray(pointsArray, xrot, yrot, 0);
	//convert the 3D points to 2D points
	screenPoints = Universe3D.toUniverse2D();
	for (var i = 0; i < screenPoints.length; i++) {
		var cur_ball = this["ball" + i];
		//attach a ball to each point
		screenPoints[i].moveClipHere(cur_ball);
	}
};
space.onEnterFrame = rotateCube;

And this is the content of the customFunctions.as file

Object.prototype.toString = function() {
	//set empty string to be displayed
	var string = "";
	//cycle through properties and functions
	for (var i in this) {
		//if not a function
		if (typeof (this[i]) != "function") {
			//add the property name and value to the string
			string += i+":"+this[i].toString()+", ";
		} else {
			//else add the function name and "Function"
			string += i+":Function, ";
		}
	}
	//don't include last comma and space in final string
	string = string.substring(0, string.length-2);
	return string;
};
MovieClip.prototype.lineHitTest = function(x1, y1, x2, y2, pre) {
	//find difference in x
	var x = x2-x1;
	//find difference in y
	var y = y2-y1;
	//find distance between points
	var dis = Math.sqrt(x*x+y*y);
	//when pre(precision) equals 2, it checks every 2 points
	//therefore, the number of point equals dis/pre
	var num = dis/pre;
	var xinc = x/num;
	var yinc = y/num;
	//vars to hold current point
	var tx, ty;
	if (num) {
		for (var i = 0; i<=num; i++) {
			//if pre equals 2 and distance is ten, then 
			//we find a point 1/5 of the way down the line.
			tx = x1+i*xinc;
			ty = y1+i*yinc;
			//if the point(tx, ty) touches the movieclip we're testing
			if (this.hitTest(tx, ty, true)) {
				//return true because the movieclip is on the line
				return {x:tx, y:ty};
			}
		}
	}
	//return false because none of the tested points hit the movieclip   
	return false;
};
Object.prototype.clone = function() {
	//empty object
	var temp = new Object();
	//for each propety in the going-to-be-cloned object
	for (i in this) {
		//copy property name with value to new object
		temp[i] = this[i];
	}
	return temp;
};
Array.prototype.clone = function() {
	//empty array
	var temp = new Array();
	//for each value in the going-to-be-cloned array
	for (i in this) {
		//copy value with index to new object
		temp[i] = this[i];
	}
	return temp;
};
Array.prototype.sum = function() {
	//empty num
	var total = 0;
	//for each value in the array
	for (i=0; itop) {
		return top;
	}
	return num;
};

Move the mouse to rotate the cube

Can be useful for a preloader, or a screen saver, or maybe you'll find another use of it.

Take the source code and enjoy

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers