0
\$\begingroup\$

I have an array of nodes, i need to get a specific Node from this array in the InputListener event using Actor hit()..

I have multiple menu items (Buttons, Levels) but the touch event not working. This is what i tried so far:

 public Actor hit(final float x, final float y, final boolean b) {
    ...
        for (int i = this.nodesList.size() - 1; i >= 0; --i) {
            final Node node = this.nodesList.get(i);
            if (node instanceof SpriteNode) {
                SpriteNode newNode = (SpriteNode) node;
                if  (newNode.anchornsform.det() != 0.0f) {
                    final Affine2 inv = newNode.anchoreansform.inv();
                  ...
                }
            }
        }
    }
    return myactor;
}

Please help, if you have a better solution I'm open to any suggestions.

Thank you

\$\endgroup\$
4
  • \$\begingroup\$ First of all, you don't need to call node != null after you checked it with instanceof (if it would be null instanceof would return false). Second, you're casting your Node object to SpriteNode 3 times. Don't. Assign it to a pointer like if(node instanceof SpriteNode) SpriteNode newNode = (SpriteNode) node; and use the newNode later. Third, I don't know what do you want to achieve. Please explain your question a bit. We don't even know if your code works now or not ;) \$\endgroup\$
    – Jacob
    Nov 27 '16 at 22:26
  • \$\begingroup\$ my code returns null \$\endgroup\$ Nov 27 '16 at 22:28
  • \$\begingroup\$ @spectre I have multiple menu items (Buttons, Levels) but the touch event not working for me \$\endgroup\$ Nov 27 '16 at 22:34
  • \$\begingroup\$ Check the answer \$\endgroup\$
    – Jacob
    Nov 27 '16 at 22:34
1
\$\begingroup\$

As you wrote in the comment, your hit() is returning null

This part:

} else {
        for (int i = nodesList.size() - 1; i >= 0; --i) {
            final Node node = nodesList.get(i);
            if (node instanceof SpriteNode) {
                if (node != null && ((SpriteNode)node).anchoredAffineWorldTransform.det() != 0.0f) {
                    final Affine2 inv = ((SpriteNode)node).anchoredAffineWorldTransform.inv();
                    if (((SpriteNode)node).hit(inv.m00 * x + inv.m01 * y + inv.m02, inv.m10 * x + inv.m11 * y + inv.m12, b) != null) {
                        myactor= node;
                        return myactor;
                    }
                }
            }
        }
    }

can't return null - cause it just can't.

So either myactor = super.hit(x, y, b); is causing null or else isn't fired at all and you have null cause nothing was assigned to myactor.

\$\endgroup\$
6
  • \$\begingroup\$ the touch event still not working \$\endgroup\$ Nov 27 '16 at 22:38
  • \$\begingroup\$ Check with debugger and breakpoints if this line: if (newNode.hit(inv.m00 * x + inv.m01 * y + inv.m02, inv.m10 * x + inv.m11 * y + inv.m12, b) != null) { is ever fired. If it is - it's the bugged line. \$\endgroup\$
    – Jacob
    Nov 27 '16 at 22:40
  • \$\begingroup\$ no its not fired, please check this pic : i.stack.imgur.com/Awg5j.png \$\endgroup\$ Nov 27 '16 at 22:46
  • \$\begingroup\$ Sorry @spectre the line myactor= node; is the one not fired \$\endgroup\$ Nov 27 '16 at 23:06
  • \$\begingroup\$ To be 100% clear, you have your error here: inv.m00 * x + inv.m01 * y + inv.m02, inv.m10 * x + inv.m11 * y + inv.m12, b. Check it again. I can't do anything cause I don't even know how these parameters should work. \$\endgroup\$
    – Jacob
    Nov 28 '16 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.