You Are Here:

Community: Developer Discussion Boards

#1 Old How to recognize a Layer - 2009-03-12, 14:26

Join Date: Apr 2007
Posts: 1,757
Tiger79's Avatar
Tiger79
Offline
Forum Nokia Champion
Hi,
I am struggling to retrieve what kind of Class a specific Layer is as used in the LayerManager API...
You can add Sprites to a LayerManager which become instances of Layer... Let's say I've got 3 custom Sprite derivates, classes Apple, Pear and Banana...
Lets say I add 3 Apples, 2, Pears and one Banana to the LayerManager...
How do I recognize a Pear object afterwards ? Lets say I want to remove only the Pear elements, how can that be achieved ?
Thanks in advance
Reply With Quote

#2 Old Re: How to recognize a Layer - 2009-03-12, 15:00

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
Tut... you know this! Sprites don't become instances of Layer... they already are (Sprite extends Layer). You can tell what specific type a Layer is, by using instanceof.

PHP Code:
Layer layer layerManager.getLayerAt(n);
if (
layer instanceof Sprite) {
    
Sprite sprite = (Sprite)layer;

Same deal with any subclass of Sprite you create.

PHP Code:
// remove Pears (Pear extends Sprite)
for (int i 0layerManager.getSize(); ) {
    
Layer layer layerManager.getLayerAt(i);
    if (
layer instanceof Pear) {
        
layerManager.remove(layer);
    } else {
        
i++;
    }

Or... is this what you're doing and it doesn't work? :)

Cheers,
Graham.
Reply With Quote

#3 Old Re: How to recognize a Layer - 2009-03-12, 15:11

Join Date: Apr 2007
Posts: 1,757
Tiger79's Avatar
Tiger79
Offline
Forum Nokia Champion
hehe,
thx Graham, it seems to work...
I was trying something a little too complex, my algorithm had one extra step in which instead of checking the retreived Layer to the needed class I first extracted the Object from the Layer and then did an instance of check, which actually didnt work :(

btw great if-else loop, already taking into account that if a Layer is removed the indexes within the layermanager decrease by one and thus no i++ ;)
another way (which I used till now) was the opposite, starting out at the end of the Layers stack (highest index) and working downwards...

anyways thx graham, as always u have been of great help :D

"Tut... you know this!".... ehhrmm... yeah I knew that, was just a momentary lapse of reason :P or the lack of that :D
Reply With Quote

#4 Old Re: How to recognize a Layer - 2009-03-12, 15:48

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
Quote:
Originally Posted by Tiger79 View Post
another way (which I used till now) was the opposite, starting out at the end of the Layers stack (highest index) and working downwards...
Like this:

PHP Code:
// remove Pears (Pear extends Sprite), Tiger69-style
for (int i layerManager.getSize() - 1>= 0i--) {
    
Layer layer layerManager.getLayerAt(i);
    if (
layer instanceof Pear) {
        
layerManager.remove(layer);
    }

Sure, this is better, since you avoid a (potentially expensive) method call on every iteration. For some reason, I have an "issue" with loops that go backwards. They seem to confuse me somehow. :)

Cheers,
Graham.
Reply With Quote

#5 Old Re: How to recognize a Layer - 2009-03-12, 15:55

Join Date: Apr 2007
Posts: 1,757
Tiger79's Avatar
Tiger79
Offline
Forum Nokia Champion
hehe well if we have to start tweaking and optimizing I would declare Layer layer outside the loop,so that it gets reutilized instead of creating a new Object every time :

PHP Code:
Layer layer;
for (
int i layerManager.getSize() - 1>= 0i--) 
{
    
layer layerManager.getLayerAt(i);
    if (
layer instanceof Pear
    {
        
layerManager.remove(layer);
    }

or doesnt that make any sense ?
U know how Pears are, they are pretty complex fruits ;) they come in different sizes, colors, countries, wights and so forth, and not to forget they come in bunch :P So like this I might be saving some memory by not instanciating a Layer object every time :P

hehe btw I dont know what u were thinking about when u typed this
Quote:
Tiger69-style
hehe
Reply With Quote

#6 Old Re: How to recognize a Layer - 2009-03-12, 16:28

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
Quote:
Originally Posted by Tiger79 View Post
or doesnt that make any sense ?
Again, tut! :) That's just a declaration, it doesn't allocate memory or run any code.

What it does do, is tell the compiler to allocate a slot in the stack-frame for the variable. Since that slot only needs to be reserved for the scope of the variable (it's enclosing {...}), nesting the declaration as deep as possible keeps the scope small, and allows the compiler to re-use the stack-frame slot for another local variable later.

PHP Code:
// this requires three slots, one for each i, j and o
public static void a() {
    
int i;
    
Integer j;
    
Object o;
    for (
010i++) {
        
= new Integer(i);
        
myVector.addElement(j);
    }
    if (
System.currentTimeMillis() > 0) {
        
= new Object();
        
myVector.addElement(o);
    }
}

// this only requires two slots
public static void b() {
    for (
int i 010i++) {
        
// two slots in here, for i and j
        
Integer j = new Integer(i);
        
myVector.addElement(j);
    }
    if (
System.currentTimeMillis() > 0) {
        
// one slot here, for o
        
Object o = new Object();
        
myVector.addElement(o);
    }

Re-using slots keeps the frame size down, so saves some memory. But not much. More usefully, the first four frame slots are accessed by shorter byte-code instructions, so you reduce the code size, and potentially increase performance. In an instance method, slot zero is always "this".

More significantly, you could also be preventing an object from becoming garbage, by keeping a reference to it in a variable you no longer want. By letting the variable fall out of scope, you increase the chances of the garbage collector will notice that the object is unreferenced, and release the memory.

So, as a rule, keep the declaration as close to the use as possible.

Quote:
Originally Posted by Tiger79 View Post
I dont know what u were thinking about when u typed this
Someone called you that in another post... you were amused, but I think you secretly like it... :P
Reply With Quote

#7 Old Re: How to recognize a Layer - 2009-03-12, 17:19

Join Date: Apr 2007
Posts: 1,757
Tiger79's Avatar
Tiger79
Offline
Forum Nokia Champion
hhmmm,
ok, thanks for the insight, that about the slots I didn't know actually...
well thats another lesson, still enough to learn, and it's good to have people in this forum which can give such extended information regarding not only the devices but Java specifically as a language...
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
Layer ads break browsing nokiameister General Browsing 0 2007-06-11 12:39
graphic layer over a video layer ninku6a Symbian Media (Graphics & Sounds) 0 2006-02-21 14:38
graphic layer over a video layer ninku6a Mobile Java Media (Graphics & Sounds) 0 2006-02-21 14:37
::: How much tiled layers on Nokias60? (speed Q) ::: alex_raider Mobile Java Media (Graphics & Sounds) 0 2005-12-09 19:26
MMS (WAP) Layer description Potappel General Messaging 0 2003-10-23 10:22

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d18645X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ