Created
February 9, 2014 13:21
-
-
Save gregkepler/8898982 to your computer and use it in GitHub Desktop.
Timed Event loops - AS3 vs Cinder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "cinder/app/AppBasic.h" | |
#include "cinder/gl/gl.h" | |
#include "cinder/Timeline.h" | |
#include "cinder/Rand.h" | |
using namespace ci; | |
using namespace ci::app; | |
using namespace std; | |
class CinderEventLoopApp : public AppBasic { | |
public: | |
void setup(); | |
void mouseDown( MouseEvent event ); | |
void update(); | |
void draw(); | |
void timerCalled(); | |
CueRef mCue; | |
bool mGoing; | |
Anim<Vec2f> mNewPos; | |
}; | |
void CinderEventLoopApp::setup() | |
{ | |
mGoing = true; | |
//osx | |
mCue = timeline().add( bind( &CinderEventLoopApp::timerCalled, this), timeline().getCurrentTime() + 1 ); | |
//windows | |
/*std::function<void()> a = std::bind(&CinderEventLoopApp::timerCalled, this); | |
mCue = timeline().add(a, timeline().getCurrentTime() + 1);*/ | |
mCue->setDuration( 1 ); | |
mCue->setAutoRemove( false ); | |
mCue->setLoop(); | |
} | |
void CinderEventLoopApp::timerCalled() | |
{ | |
Vec2f randomPos( randFloat() * getWindowWidth(), randFloat() * getWindowHeight() ); | |
timeline().apply( &mNewPos, randomPos, 1, EaseInOutQuad() ); | |
} | |
void CinderEventLoopApp::mouseDown( MouseEvent event ) | |
{ | |
mGoing = !mGoing; | |
if(mGoing){ | |
mCue->setLoop(); // start | |
}else{ | |
mCue->setLoop( false ); //stop | |
} | |
} | |
void CinderEventLoopApp::update() | |
{ | |
} | |
void CinderEventLoopApp::draw() | |
{ | |
// clear out the window with black | |
gl::clear( Color( 0, 0, 0 ) ); | |
glPushMatrix(); | |
gl::translate( mNewPos.value() ); | |
gl::color( 1, 0, 0, 1 ); | |
gl::drawSolidRect( Rectf( -50, -50, 50, 50 ) ); | |
glPopMatrix(); | |
} | |
CINDER_APP_BASIC( CinderEventLoopApp, RendererGl ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flash.utils.Timer; | |
import flash.events.TimerEvent; | |
import flash.display.Sprite; | |
import fl.transitions.Tween; | |
import fl.transitions.easing.Regular; | |
import flash.events.MouseEvent; | |
// set up the timer | |
var timer:Timer = new Timer(1000); | |
timer.addEventListener(TimerEvent.TIMER, onTimerTick); | |
// set up the sprite that moves when the timer is called | |
var box:Sprite = new Sprite(); | |
box.graphics.beginFill(0xff0000); | |
box.graphics.drawRect(-50, -50, 100, 100); | |
box.graphics.endFill(); | |
addChild(box); | |
var going:Boolean = true; | |
// this is what happens each timer event | |
function onTimerTick( event:TimerEvent ) { | |
var boxTweenX:Tween = new Tween(box, "x", Regular.easeOut, box.x, Math.random()*stage.stageWidth, .9, true); | |
var boxTweenY:Tween = new Tween(box, "y", Regular.easeOut, box.y, Math.random()*stage.stageHeight, .9, true); | |
} | |
// start the timer | |
timer.start(); | |
// Click to start/stop the timer | |
stage.addEventListener(MouseEvent.CLICK, onClick); | |
function onClick(event:MouseEvent) | |
{ | |
going = !going; // Toggle true/false | |
if(going){ | |
timer.start(); | |
}else{ | |
timer.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment