Lively Kernel canvas
Example3/3301
LetthemicedanceNowassembleallthemethodstogethersothatyoucanaddthemasapreambletotheChangeSet.Thiswillallowyoutosavethepagewiththerunningmiceandloaditagain.Allthesourceafteralittlecleanup:3
Selectit,openamenuwithalt+click,goto'TextFunctions...'->'addaspreamble...'.Saveyourpageandreloadit.Evaluate'Mouse'.Itshouldnotbeundefined.Youcannowcreateafewmice,callstartSteppingScriptsonthemandsaveyourpage.Congratulations,youhavejustgivenafewcutemiceanewhome!Tobecontinued.'add as preamble...'. Save your page and reload it. Evaluate 'Mouse'. It should not be undefined.\nYou can now create a few mice, call startSteppingScripts on them and save your page.\nCongratulations, you have just given a few cute mice a new home!\n\nTo be continued."]]>
Previous22
0.160163.0395958025507-1.293782906481597
10007.433861299433536Wikicontroltrue
nullgetRandomNumber=function(max){returnMath.floor(Math.random()*max+1)-1};ImageMorph.subclass('Mouse',{howFarCanIGoWithOneStep:5,maxTurn:Math.PI/2,initialize:function($super){//calltheinitializeofImageMorphwiththemousepicture$super(newRectangle(45,45,49,49),'http://www.lively-kernel.org/repository/lively-wiki/mouse.png');//movepositiontothecenterofthemorphinsteadoftopleftcorner,nicerrotatingbehaviorthis.moveOriginBy(this.getExtent().scaleBy(0.5));//removethebluebackgroundthis.setFill(null);},calculateNewPosition:function(){varp=this.getPosition();varangle=this.getRotation()+Math.PI*(1/2);varr=this.howFarCanIGoWithOneStep;returnpt(p.x+r*Math.cos(angle),p.y+r*Math.sin(angle));},moveOneStep:function(){this.mayBeChangeDirection();varnewPos=this.calculateNewPosition();while(!WorldMorph.current().bounds().containsPoint(newPos)){this.newDirection();newPos=this.calculateNewPosition();}this.setPosition(newPos);},mayBeChangeDirection:function(){if(getRandomNumber(10)==5)this.newDirection()},newDirection:function(){this.rotateBy(getRandomNumber(this.maxTurn*100)/100-this.maxTurn/2);},startSteppingScripts:function(){this.startStepping(20,'moveOneStep')}});33truetruefalsetruefalse
false1truefalsenulltruefalse
nullfalsenullfalse
Thecompletesourcecodeuptonownullfalse
false
false
falsenullfalsefalsenullnullnullnullfalsenull26