
It's very useful that we can comunicate something from a "project" inside an island to an object inside the same island.
The exercice of this lesson is simple. A button, inside a project will start or stop the rotation of our red cube.
The first concept to know is that of Class Variable that is a variable that will have the same value at any instantialization of the Class - at some languages this kind of element is static.
Let's go to modify the definition of the Class RedCube. Needs to be uppercased:
TVehicle subclass: #RedCube instanceVariableNames: 'rc' classVariableNames: 'Flag' poolDictionaries: '' category: 'DMUcourse'
Another important thing to know is how we define a property in SmallTalk. We realy will have two methods, one to assign a value and another to "read" the value. In the same RedCube Class we create:
flag ^ Flag.
And the other:
flag:aFlag. Flag:= aFlag.
We need to define an initial value for the flag at the "initialize" of HosterWorld:
... "Install the red cube" rcOnBoard := RedCube new. rcOnBoard flag:1. rcOnBoard translationX: 0 y:-140 z:0. space addChild: rcOnBoard. ...
Using World | open | morphic project we open a page for a new project. We put a Button in the page and, opening the Viewer, we drag an Script Editor:
The script that will run will have an IF-THEN-ELSE block, changing the static variable Flag of the Class RedCube:
| rc3D |
rc3D:=RedCube new.
(rc3D flag == 1)ifTrue: [
rc3D flag:0.
]
ifFalse: [
rc3D flag:1.
].
rc3D:= nil.
We are creating a new instance of the Class RedCube (will not appear any red cube at any place...) and change the value of Flag. Pay attention that we are using a method to assign a value to the variable.
NOTE OF THE BETA VERSION: Like "mouseDown" is not working, here we can use "mouseUp"
Clean the page and "save project on file" (name: FlipFlop) and "big save".
Back to the main page (World | previous project) we need to change the method stepAt of the Class RedCube (our time-step-loop):
stepAt: currentTime | theFlag | theFlag:= self flag. (theFlag==1)ifTrue: [ rc addRotationAroundY: 1. ] ifFalse: [ rc addRotationAroundY: 0. "stops the rotation" ]. (theFlag==1)ifTrue: [ rc addRotationAroundY: 1. ] ifFalse: [ rc addRotationAroundY: 0. "stops the rotation" ].
If we run the Hoster we will see the project FlipFlop in the Dock. Click it. You can move, change the size etc. of the window.
When you click the button you will se the results in the cube.
Will this work from other image and in the Navigator? Only if the image has all the same Classes versions, projects etc.
IMPORTANT WARNINGS: If you are creating a not very simple project it's possible that you will make corrections and resave it. Squeak has a confuse system of "versioning". It changes the names of some elements and your code can not work anymore. Reopen the Viewer of all the objects to see if this change ocurred and change the code. Better: when you have a final version, recreate all the project with another name. It's not practical, but is our sugestion...
Remember that you can not save Croquet with any island open. Like the Hoster is for 24/7 work, the load of a project using graphic resource, like we did, is not a problem.
PREVIOUS LESSON NEXT LESSON T. CONTENTS HOMEPAGE
DISCLAIMER: This material can be translated for any language, and reproduced total or partially by anybody using any type of media. But, please, don't put your name like author. And let me know if it was useful (americo@dmu.com).The use of any code, 3D model or technique published is free and doesn't need to have any reference about this source. The author is not responsible for any damage that the material can cause.
