AndEngine and Android: OnAreaTouched in a for loop not remaining unique - java

I'm using andengine for android for the first time. I'm creating a simple list of text that simply needs to be able to call the same method but send a different variable accroding to its position on the list. The list does not need to scroll and is short. I decided to use simple text and created a for loop to make them for me from a string array.
The loop works and creates the text properly but when touching they all perform the last touch assignment instead of the corresponding one.
Does the onAreaTouched code need to be referred to or is it saved some where once the touch area is registered.
here it is:
for(x =1; x<base.locale.length;x++){
textLoc[x-1]=new Text(10,(110+(x*30)),base.getmFont(),
base.locale[x],HorizontalAlign.CENTER){
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
base.moveListen(Integer.toString(x));
loadNewScene();
return true;
}
};
scene.getLastChild().attachChild(textLoc[x]);
scene.registerTouchArea(textLoc[x]);
}

Does that code compile? As in this line:
base.moveListen(Integer.toString(x));
you are refering to the non-final loop-variable x.

Related

Clearing textbox from StdDraw to write new input

I'm not sure if this would be a simple question to answer on here, just because I'm using the Standard Draw class written by Princeton University, and I'm not sure if it's a globally known class.
But I'd much appreciate any feedback from those familiar with the StdDraw library.
What I'm trying to do is fairly straight-forward; check to see if the mouse of the user clicks onto an input box I drew, and if it is clicked, clear the existing text (which simply says "input") to make an empty String.
This is what it looks like so far:
public boolean handleClick(double x, double y) {
if(!super.handleClick(x,y)){
value = false;}
else {
if(highlighted){
value = true;
StdDraw.textLeft(xCentre+0.005,yCentre," ");} //Add the label
else{
value = false;}
}
return value; //I handled it. Nobody else should.}
}//handleClick
super.handleClick(x,y) is simply a method in the super class that draws the dimensions of the box:
public void draw(){
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle(xCentre,yCentre,halfWidth,halfHeight);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(); //Default thin line.
StdDraw.rectangle(xCentre,yCentre,halfWidth,halfHeight);
}
value is simply an instance variable of this class that will return true if all conditions are satisfied:
private boolean value;
highlighted is a boolean instance variable from the super class that simply states if the box is an input or output box.
My main question would be, is the line
StdDraw.textLeft(xCentre+0.005,yCentre," ");
the right way to clear the existing text and create an empty String with StdDraw? As it's not clearing the line, but maybe there's a bug elsewhere in my code that I'm missing and this line should work?

Comparing components via setName() .

I am coding an image puzzle game and one part of the code is to compare the pieces the user has selected to the pieces of the correct image.
Each image piece is already added to a JButton as an ImageIcon.
An identifier is required to differentiate each image piece apart and also for comparision.
I am setting a setName() for each JButton created as the identifier.
The comparison starts when the user releases the mouse after he drags the puzzle pieces from the original 3x3 grid where the shuffled pieces are to the other 3x grid for matching.
I have problems removing the error from the comparison if statement.
I got the comparison idea from this SO thread - link
private JButton[] button = new JButton[9];
private JButton[] waa = new JButton[9];
private String id;
private int cc;
private String id2;
private int cc2;
// setName for each of the 9 buttons in the original 3x3 grid being created
// which stores the shuffled puzzle pieces
for(int a=0; a<9; a++){
button[a] = new JButton(new ImageIcon());
id += Integer.toString(++cc);
button[a].setName(id);
}
// setName for each of the 9 buttons in the other 3x3 grid
// where the images will be dragged to by the user
for(int b=0; b<9; b++){
waa[b] = new JButton();
id2 += Integer.toString(++cc2);
waa[b].setName(id2);
}
// check if puzzle pieces are matched in the correct place
// compare name of original 'button' array button with the name of 'waa' array buttons
button[a].addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent m){
if(m.getbutton().getName().equals (waa.getName())){
}
else{
JOptionPane.showMessageDialog(null,"Wrong! Try Again.");
}
}
}
In your mouseReleased event m.getButton() is returning the mouse button that was clicked. You'll want to do something more like this that will get you closer:
if (m.getComponent().getName().equals(waa.getName())) {
m.getComponent() returns the Component object (your JButton) that the event was fired from. From there you can do the comparison with the getName approach you are using.
There's an additional issue in that your waa variable is an array. I'm not sure how you want to compare them, whether running through the arrays and making sure the index and names match, but that's an additional issue you need to look into.
JButton uses an ActionListener to trigger notifications back to your program to indicate when it's been triggered. This allows the button to respond to different types of event, including the mouse, keyboard and program triggers.
As apart of the action API, you can supply an action command for each button. See JButton#setActionCommand
Basically you would integrate it in a simular way to your mouse listener...
public void actio Performed(ActionEvent evt) {
if (command.equals(evt.getActionCommand()) {...}
}
Depending on your requirements, it might even be easier to use the Action API
The problem you are actually having is waa is an array, therefore, it doesn't have a getName method. I'm also unclear as to why you have two arrays of buttons?

Should I implement actions inside the methods of an InputListener object?

I am making a button using MouseOverArea. After some trial and error, I realized I can override the methods in InputListener to do particular actions when an input event is notified.
For example, do things when mouse left button is pressed while cursor is over the component.
#Override
public void mousePressed(int button, int mx, int my) {
if (isMouseOver() && button == Input.MOUSE_LEFT_BUTTON) {
// Some magic happens
}
}
However, I will not able to do things like changing current game state because no Game object around. I know there are many ways to solve this problem, but I would like to know what is the Slick way to do this.
Are these methods suitable for such behavior ?
One way to modify game states is to use boolean states; Which are boolean variables that hold the state of the game or player. For example:
boolean isMovingUp, isMovingLeft, isMovingRight, isMovingDown;
You can then set these to true/false depending on what mouse or keyboard event takes place and your game class then read these variables, like so:
if (isMovingUp) {
// do something
isMovingUp = !isMovingUp;
}
Hope that helps!

Loading next level with button click

i am using AndEngine.
I have a scene that i would like to reuse for all of my levels.
The only thing that will change is the objects loaded on the scene through a method i created to load the level.
The problem i am facing is that at the end of the level the user gets the option to continue to the next level. I allow the user to load the next level with this method.
this.nextlevel = new Sprite(0,0,150,80,this.nextLevelRegion,this.getVertexBufferObjectManager()){
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final float pTouchAreaLocalX,
final float pTouchAreaLoca) {
GameScene.detachChildren();
loadLevel(level);
return true;
}
};
The loadLevel(int level_number) takes a int as a parameter. I increase this variable by level++; after the current level is loaded.
The problem is when the sprite is clicked it keeps adding +1 to the level integer and trying to load the wrong level..
Anyone know a better way of doing this?
this is what i did in my game:
i created a class that creates the game play scene, this class has a method that creates the scene and loads an XML level based on the number that it recieves when i call it.
when i want to go to next level i get the current level number (lets say its x) and call the method with x+1 as an attribute for it.
you should note that i depend in this method on recreating the scene all over again so you should reset every thing when you do that.
this is the call for the method:
Sprite Next=new Sprite(x-48/factor, y+30/factor, 96/factor, 60/factor,LoadAssets.WinMenuNextTextureRegion){
#Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionUp()){
pEngine.setScene(GamePlayScene.CreateTheGamePlayScene(context,pEngine,camera_width, camera_height,GamePlayScene.LevelNumber+1));
}
return true;
}};
and here is the link for my game if you want to see how that works:
https://market.android.com/details?id=com.ayham.blowangryshapes
i only have one game play scene in the game and the levels are loaded as needed.

How to make rect.intersects just check outside boundaries

I am trying to determine if clickRectangle, a rectangle created by the click of a mouse, on a canvas intersects with a drawnRectangle or not.
However, I don't want the intersects method to return true if I click anywhere in the drawnRectangle (i.e. the interior of the rectangle), I just want it to return true if an outer boundary of drawnRectangle was clicked.
How can I do that?
P.S: For clickRectangle and drawnRectangle see my comment below.
If I understand the question correctly, you want
public static boolean pointNearEdge(Point click, Rectangle drawnRectangle, int howNear){
Rectangle clickRect = new Rectangle(click.x-howNear, click.y-howNear, howNear*2, howNear*2);
if (drawnRectangle.contains(clickRect)) // totally inside -> false
return false;
// test if there is a partial intersection - i.e. we are near the edge
return drawnRectangle.intersects(clickRect);
}

Categories

Resources