Hello,
is there a possibility to show/hide a block depending on the name of the sprite?
I tried with
'spriteName' = objName ifTrue ...
in ScratchSpriteMorph class block specs but objName is not known in this class. So my question is probably if there an easy way to let objName be known in this class?
Thanks csn
Offline
Thank you for your reply. Here is part of the code:
In Scratch-Objects | ScratchSpriteMorph | block specs | blockspecs
blockSpecs
| blocks |
('NXT' = objName )
ifTrue: [
blocks _ #(
'motion'
('control NXT motion' - nxtControl)
('start turning %o %O' - nxtStartM:d: L forward)
).
].
The problem is: objName is not known in "block specs"
"block specs" is a class of ScratchSpriteMorph and objName is defined as a variable for instances in ScratchSpriteMorph.
But to be honest, I don't know the difference. I'm just look at other instances an try to figure out my new blocks by looking at other blocks.
cns
Offline
csn wrote:
Thank you for your reply. Here is part of the code:
In Scratch-Objects | ScratchSpriteMorph | block specs | blockspecs
blockSpecs
| blocks |
('NXT' = objName )
ifTrue: [
blocks _ #(
'motion'
('control NXT motion' - nxtControl)
('start turning %o %O' - nxtStartM:d: L forward)
).
].
The problem is: objName is not known in "block specs"
"block specs" is a class of ScratchSpriteMorph and objName is defined as a variable for instances in ScratchSpriteMorph.
But to be honest, I don't know the difference. I'm just look at other instances an try to figure out my new blocks by looking at other blocks.
cns
hmmm... sorry but i am not really good at squeak... u might have to wait until someone better than me answers this
Offline
The problem is more complex. I tried to compare objName to a string in an instance code block:
self say: objName.
t1 <- 'NXT'.
[t1 = objName] value
ifFalse: [ ...
The first line leads to bubble with NXT. So objName is known and has the expected value. But the comparison is always False.
Is objName not a string? Is the comparison o.K.? Is there a possibility to check the sprite name?
I should know a little bit more about squeak and smalltalk I assume.
csn
Offline
I figured it out for instances but don't ask for explanation:
t1 <- #NXT.
t2 <- objName asSymbol.
t1 = t2
ifFalse: [...]
ifTrue: [...].
Now the value of t1 = t2 is True.
But the problem I started with remains. In Scratch-Objects | ScratchSpriteMorph | block specs | blockspecs >objName< is unknown.
Does anybody know how to change it?
Offline
csn wrote:
t1 <- #NXT.
t2 <- objName asSymbol.
t1 = t2
ifFalse: [...]
ifTrue: [...].
I'm learning.
String comparison:
('NXT' compare: objName) =2
ifFalse: [...]
ifTrue: [...].
is also working as expected.
csn
Offline
Unfortunately your work goes beyond the ScratchSpriteMorph class.
You need to trace back to the method that creates all the blocks, and there you can add a condition for the objName (since the method I'm talking about is a ScriptableScratchMorph which has objName defined as an instance var).
Offline
LS97 wrote:
You need to trace back to the method that creates all the blocks, and there you can add a condition for the objName (since the method I'm talking about is a ScriptableScratchMorph which has objName defined as an instance var).
But objName is still an instance var and I can't use it in the class part of ScriptableScratchMorph either. The blockspecs are defined in the class part.
I can use objName in ScriptableSpriteMorph in instances, I tried it. I think this is because ScriptableSpriteMorph seems to be a subclass of ScriptableScratchMorph.
But I'm just sort of guessing, there is no deeper knowledge.
csn
Offline
csn wrote:
LS97 wrote:
You need to trace back to the method that creates all the blocks, and there you can add a condition for the objName (since the method I'm talking about is a ScriptableScratchMorph which has objName defined as an instance var).
But objName is still an instance var and I can't use it in the class part of ScriptableScratchMorph either. The blockspecs are defined in the class part.
I can use objName in ScriptableSpriteMorph in instances, I tried it. I think this is because ScriptableSpriteMorph seems to be a subclass of ScriptableScratchMorph.
But I'm just sort of guessing, there is no deeper knowledge.
csn
You're right about not being able to access objName from the class method of ScriptableScratchMorph, because it's instance. But what I meant is looking at the method that creates the blocks -- and that's an instance method.
EDIT
Maybe I wasn't clear enough. The various blockSpecs methods are simply lists of blocks, they are not methods. They are then used by the block creating method as a list to reference!
Last edited by LS97 (2011-10-18 11:28:14)
Offline
Thanks a lot! It finally works.
In ScriptableScratchMorph | blocks | viewerPageForMotion
I inserted:
('NXT' compare: objName) = 2
ifTrue: [
(self blocksFor: 'motionNXT') do: [...].
]
ifFalse: [
(self blocksFor: 'motion') do: [ ...].
].
instead of
(self blocksFor: 'motionNXT') do: [...].
Then I made an extra blocklist for motionNXT and it works. Now I just have to find the place where the block-color is set. I have seen it these days. I will get this.
Thanks a lot LS97!
csn
Offline
Very good! That's exactly what I was suggesting!
As an added bonus, you might want to say
if name is NXT, add blocks for nxt motion.
regardless of what sprite it is, add the default motion category.
to avoid having to duplicate the motion category
Offline