Login  Register

Re: Question on Morphic drawOn: method.

Posted by HilaireFernandes on Feb 04, 2015; 7:26pm
URL: https://forum.world.st/Question-on-Morphic-drawOn-method-tp4803695p4803730.html

Hello Nacho,

It is even simpler as you seems to imagine.
First you don't need to subclass EllipseMorph if all you want is a ball, then you don't need the position attribute, Morph always knows about positionning.
Then, the #drawOn: method is to do the drawing, you don't need to subclass as long as you don't need a different drawing. Color, border should be set elsewhere.

The redbox with the cross appears in a Morph area when its drawing gets wrong, here your override in drawOn:

For inspiration you can copy and paste the code example bellow; as you mentioned animation, it shows you a few tips on that matter.

Good exploration

Hilaire


| aBall |
aBall := EllipseMorph  new.
aBall borderWidth: 10;
    borderColor: Color green;
    color: Color white;
    extent: 100@100.
aBall position: ( 10@100 ).
aBall openInWorld.
100 to: 50 by: -1 do: [ :x |
    aBall
        height: x;
        bottom: 200 - x.
    World doOneCycle]


Le 04/02/2015 19:00, nacho a écrit :
Hi,
I have the following question:
I want to draw a bouncing ball.
First thing I do is create a class:

EllipseMorph subclass: #Ball
	instanceVariableNames: 'position'
	classVariableNames: ''
	category: 'PBE-BouncingBall'


Then an initialization method that mostly do a super initialize.
Then the drawOn:  
  
drawOn: aCanvas
	aCanvas borderWidth:10; borderColor: Color green.


And finally a position method:

position: aPoint
	super position: aPoint.

position
	^ position.


Finally in a Playground I do:

| aBall |

aBall := Ball new.
aBall position: ( 10@10 ).
aBall openInWorld.

But I get a red box with two crossing yellow lines.
What I'm missing?

Thanks in advance.
Nacho




-----
Nacho
Smalltalker apprentice.
Buenos Aires, Argentina.
--
View this message in context: http://forum.world.st/Question-on-Morphic-drawOn-method-tp4803695.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.




-- 
Dr. Geo - http://drgeo.eu
iStoa - http://istoa.drgeo.eu