My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
as3isolib_tutorial_007  
Quick example showing how to animate a scene
Featured, Phase-Implementation, tutorial
Updated Feb 4, 2010 by jwopitz

prerequisites

This example makes use of Grant Skinner's GTween. You can download the source code here - link.

tutorial

Most developers will seek some level of interactivity with the as3isolib and the content created with it. Here is a quick example on how to make a simple interactive scene using a grid and a box. By clicking on the any of the cells in the grid, the box is tweened to that location.

example swf

code

package
{	
	import as3isolib.core.IIsoDisplayObject;
	import as3isolib.display.IsoView;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	
	import com.gskinner.motion.GTween;
	
	import eDpLib.events.ProxyEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class IsoApplication extends Sprite
	{
		private var box:IIsoDisplayObject;
		private var scene:IsoScene;
		
		public function IsoApplication ()
		{
			scene = new IsoScene();
			
			var g:IsoGrid = new IsoGrid();
			g.showOrigin = false;
			g.addEventListener(MouseEvent.CLICK, grid_mouseHandler);
			scene.addChild(g);
			
			box = new IsoBox();
			box.setSize(25, 25, 25);
			scene.addChild(box);
			
			var view:IsoView = new IsoView();
			view.clipContent = false;
			view.y = 50;
			view.setSize(150, 100);
			view.scene = scene; //look in the future to be able to add more scenes
			addChild(view);
			
			scene.render();
		}
		
		private var gt:GTween;
		
		private function grid_mouseHandler (evt:ProxyEvent):void
		{
			var mEvt:MouseEvent = MouseEvent(evt.targetEvent);
			var pt:Pt = new Pt(mEvt.localX, mEvt.localY);
			IsoMath.screenToIso(pt);
			
			if (gt)
				gt.end();
			
			else
			{
				gt = new GTween();
				gt.addEventListener(Event.COMPLETE, gt_completeHandler);
			}
			
			gt.target = box;
			gt.duration = 0.5;
			gt.setProperties({x:pt.x, y:pt.y});
			gt.play();
			
			if (!hasEventListener(Event.ENTER_FRAME))
				this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		private function gt_completeHandler (evt:Event):void
		{
			this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		private function enterFrameHandler (evt:Event):void
		{
			scene.render();
		}
	}
}
Comment by voodooattack, Mar 21, 2009

The line: view.scene = scene; //look in the future to be able to add more scenes Should be changed to: view.addScene(scene);

With the latest version.

Comment by rami.man...@gmail.com, Nov 1, 2009

I just have couple of questions: In case i put IsoSprites? on top of the grid , how can i still get click events? i tried to define the listener on the scene an it works, but localx and localy cordinates are wrong. The only thing i want is to be able to calculate X and Y of the tail was clicked ( even if some ISOObject is on it...).

Comment by fuad.all...@gmail.com, Nov 11, 2009

IsoApplication?.as(41): col: 30 Error: Access of possibly undefined property scene through a reference with static type as3isolib.display:IsoView?.

Comment by pingp...@gmail.com, Dec 29, 2009

I got this error:

iNode.as, Line 34 | 1045: Interface IEventDispatcherProxy was not found

Any help?

Comment by project member jonathan...@gmail.com, Jan 11, 2010

Don't download GTween's latest version, you have to download the beta2

Comment by gregoryb...@gmail.com, Feb 24, 2010

The latest version of GTween 2.1 lines

gt.setProperties({x:pt.x, y:pt.y}); gt.play();

I believe should be

gt.setValues({x:pt.x, y:pt.y}); gt.paused = false;

Comment by gregoryb...@gmail.com, Mar 7, 2010

To add a shadow add lines//<-

box = new IsoBox?(); box.setSize(25, 25, 25); box.moveBy(20, 20, 15);//<- scene.addChild(box);
var factory:as3isolib.core.ClassFactory? = new as3isolib.core.ClassFactory?(DefaultShadowRenderer?); //<- factory.properties = {shadowColor:0x000000, shadowAlpha:0.15, drawAll:false};//<-
scene.styleRenderers = factory?;//<-
and add imports
import as3isolib.core.ClassFactory?; import as3isolib.core.IFactory; import as3isolib.display.renderers.DefaultShadowRenderer?;

Comment by cn.it...@gmail.com, Mar 10, 2010

why add IsoView?

Comment by george.t...@gmail.com, Mar 18, 2010

Error: Type was not found or was not a compile-time constant: ProxyEvent?.

any help?

Comment by hujun36...@gmail.com, Mar 22, 2010

I have used TweenLite? to animate it :

source codes :

package {

// import as3isolib.core.IIsoDisplayObject;

import as3isolib.display.IsoView?;
import as3isolib.display.primitive.IsoBox?;
import as3isolib.display.scene.IsoGrid?;
import as3isolib.display.scene.IsoScene?;
import as3isolib.geom.IsoMath?;
import as3isolib.geom.Pt;
import eDpLib.events.ProxyEvent?;
import com.greensock.TweenLite?;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent?;
public class Main extends Sprite {
private var box:IsoBox?;
private var scene:IsoScene?;
public function Main() {
scene=new IsoScene? ;
var g:IsoGrid?=new IsoGrid? ;
g.showOrigin=false;
g.addEventListener(MouseEvent?.CLICK,grid_mouseHandler);
scene.addChild(g);
box=new IsoBox? ;
box.setSize(25,25,25);
scene.addChild(box);

link: http://blog.csdn.net/hu36978/archive/2010/03/22/5405556.aspx

var view:IsoView?=new IsoView?() ; view.clipContent=false;//默认值为true view.y=50; view.setSize(150,100); view.addScene(scene);//look in the future to be able to add more scenes addChild(view);

scene.render();
}
private var tween:TweenLite?; //计数器 //private var num:int=0; private function grid_mouseHandler(Evt:ProxyEvent?):void {
var evt:MouseEvent?=MouseEvent?(Evt.targetEvent);// 转化事件类型
//将其转化为isolib里的坐标 var pt:Pt=new Pt(evt.localX,evt.localY); trace("开始的:"+pt.x); IsoMath?.screenToIso(pt);//得到转化后的isolib坐标
trace("pt为:"+pt.x);
TweenLite?.to(box,0,{x:pt.x,y:pt.y,onComplete:myFunction}); //num++; //trace(num);
}
private function myFunction():void {
scene.render();
trace("OK");
}
}

}

Comment by jayden.m...@gmail.com, Mar 30, 2010

I think I improved the effect by replacing gt.end(); with gt.nextTween; (with 2.1) I havnt read any GTween docs so I dont really know if its a good thing :).

Anyway, Ive been reading through the as3isolib tuts and dropping them into a flex app. I like what I see very very much. cant wait to attempt to make something really cool using it.

Comment by rend...@gmail.com, Apr 15, 2010

If you want to use TweenMax? instead, replace all of the GTween lines with

 TweenMax.to(box, 0.5, {x:pt.x, y:pt.y, onComplete:gt_completeHandler});

and it will work just the same.

Comment by siau...@gmail.com, Jul 13, 2010
import as3isolib.core.IIsoDisplayObject;
import as3isolib.display.IsoView;
import as3isolib.display.primitive.IsoBox;
import as3isolib.display.scene.IsoGrid;
import as3isolib.display.scene.IsoScene;
import as3isolib.geom.IsoMath;
import as3isolib.geom.Pt;

import eDpLib.events.ProxyEvent;
import com.greensock.TweenMax;

var box:IIsoDisplayObject;
var scene:IsoScene;
var tm:TweenMax;

scene = new IsoScene();

var g:IsoGrid = new IsoGrid();
g.showOrigin = false;
g.addEventListener(MouseEvent.CLICK, grid_mouseHandler);
scene.addChild(g);

box = new IsoBox();
box.setSize(25, 25, 25);
scene.addChild(box);

var view:IsoView = new IsoView();
view.clipContent = false;
view.setSize(stage.stageWidth, stage.stageHeight);
view.addScene(scene);
addChild(view);

scene.render();

function grid_mouseHandler (evt:ProxyEvent):void
{	
	var mEvt:MouseEvent = MouseEvent(evt.targetEvent);
	var pt:Pt = new Pt(mEvt.localX, mEvt.localY);
	IsoMath.screenToIso(pt);

	TweenMax.to(box, 0.5, {x:pt.x, y:pt.y, onComplete:completeCallback})		
	
	if (!hasEventListener(Event.ENTER_FRAME))
		this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}

function completeCallback():void
{
	this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}

function enterFrameHandler (evt:Event):void
{
	scene.render();
}

The above code is updated as of July 13, 2010 and intended to work with greensock’s TweenMax?.

Comment by florin.b...@gmail.com, Aug 8, 2010

That file does not exist.

Click here to go to the home page.

Comment by thomas.p...@gmail.com, Aug 12, 2010

the SWF file link is no longer valid, please update!

Comment by winky...@gmail.com, Nov 16, 2010

Siaukia's code works; I just searched for TweenMax? and grabbed the latest version of it.

Comment by rdas.gm...@gmail.com, Nov 22, 2010

Anybody else getting an issue where the box animates to a non-grid position? Looks as if there's some error in the IsoMath?.screenToIso calculation going on??

Comment by rdas.gm...@gmail.com, Nov 23, 2010

My bad. Just realized that the example allows you to click anywhere on the grid and the box will move to that pixel location. In order to move to specific grid squares, use rounding thus:

var squareSize:int = 25; // 
var gridX:int = Math.floor( pt.x / squareSize );
var gridY:int = Math.floor( pt.y / squareSize );
...
TweenMax.to(box, 0.5, {x:gridX * squareSize, y:gridY * squareSize, onComplete:completeCallback});
Comment by truong...@gmail.com, May 21, 2011

@rich...: Thanks a lot. Works like a charm.

Comment by ivm...@gmail.com, Jan 3, 2012

example.swf is unavailable


Sign in to add a comment
Powered by Google Project Hosting