Checking if two objects have the same y-axis - android

I have been creating a simple android game, but have been encountering some problems in checking if an object falling down has passed another object on the y-axis.
This is my code:
//SKULLY is the object falling down
//USER is the object that SKULLY will have to pass through to add a point
public void checkPassed()
{
if (skullY == user.getY())
{
scoreCount++;
txtScore.setText("SCORE: " + String.valueOf(scoreCount));
}
}
What I am trying to do is that, when SKULLY passes through or is equal to the y-axis of the USER then it will add a single point.
When I change the condition to > or < it works perfectly fine no matter what position USER is in. But when I put it in ==== no point is added.
If you have to downvote me, please leave a comment on why was I downvoted. Thanks in advance for any help or insight on this problem! :D

since its sound like a Game equals ("==") almost never happens.. my Suggestion is
if(skullY < user.getY() && skullY > user.getY() + 10) {
// Do Stuff here..
}
try to lower 10 as much as possible until you will get the desire result..

Related

java.lang.StackOverflowError: stack size 8MB Problem, in recursion android studio

I am currently building a minesweeper game in android studio.
I am now building a function that basically makes the numbers show ("bomb") when you "mine"(click on one of the squares)
basically, the algorithm is:
the function is getting the index of the square as parameters ,when pressing that square(imageView)
if you press on a square- it will be revealed. if it is a square with 0 mines nearby it will also be revealed and will also do the same function on all of the squars nearby. I did this function with recursion but im getting an error "java.lang.StackOverflowError: stack size 8MB".
I might be because the recursion is calling itself too much or that it is an endless loop. anyways, I dont know how to solve that. please help
(code down here:)
public void bomb(int i,int j)
{
if(i>-1&&i<8&&j>-1&&j<10)
{
board[i][j].setRevealed(true);
if(board[i][j].getNumOfMinesNearby()==1)
{
board[i][j].setPicture("#drawable/one");
board_xml[i][j].setImageResource(R.drawable.one);
}
if(board[i][j].getNumOfMinesNearby()==2)
{
board[i][j].setPicture("#drawable/two");
board_xml[i][j].setImageResource(R.drawable.two);
}
if(board[i][j].getNumOfMinesNearby()==3)
{
board[i][j].setPicture("#drawable/three");
board_xml[i][j].setImageResource(R.drawable.three);
}
if(board[i][j].getNumOfMinesNearby()==4)
{
board[i][j].setPicture("#drawable/four");
board_xml[i][j].setImageResource(R.drawable.four);
}
if(board[i][j].getNumOfMinesNearby()==0)
{
board[i][j].setPicture("#drawable/zero");
board_xml[i][j].setImageResource(R.drawable.zero);
bomb(i-1,j+1);
bomb(i-1,j);
bomb(i-1,j-1);
bomb(i,j+1);
bomb(i,j-1);
bomb(i+1,j+1);
bomb(i+1,j);
bomb(i+1,j-1);
}
}
}
You have an infinite recursion problem. Let's say you have a 0 at board[0][0] and board[0][1]. When the user presses at [0,0] it will go into the recursive case. That will call it on [0,1]. Which will call it on [0,0]. Etc. You need to alter the algorithm so it doesn't call bomb if you have already called it on that index.
The problem with your code is that you are creating an infinite loop. When you call your function for i,j it will trigger invocation for i+1,j-1, which in turn invoke the function for i-1,j+1, which is i,j (where you've started from).
You need to modify your code, so that each cell is only visited once. Recursion should also be avoided. Better to do it in a for loop:
for(int i=0;i<ni;i++) {
for(int j=0;j<nj;j++) {
// process cell here
}
}

Cardboard Android

Is it possible to focus and open up the information just like we do on click using magnet in cardboard android?
Like ray gaze in unity, is there a alternative for android?
I want to do something like the one shown in chromeexperiments
Finally solved!
In treasurehunt sample, you can find isLookingAtObject() method which detects where user is looking...
And another method called onNewFrame which performs some action in each frame...
My solution for our problem is:
in onNewFrame method I've added this snippet code:
if (isLookingAtObject()) {
selecting++; // selecting is an integer defined as a field with zero value!
} else {
selecting = 0;
}
if (selecting == 100) {
startYourFunction(); // edit it on your own
selecting = 0;
}
}
So when user gazes 100 frame at object, your function calls and if user's gaze finishes before selecting reaches 100, selecting will reset to zero.
Hope that this also works for you
Hope this helps. (Did small research, (fingers crossed) whether the link shared below directly answers your question)
You could check GazeInputModule.cs from GoogleSamples for
Cardboard-Unity from Github. As the documentation of that class says:
This script provides an implemention of Unity's BaseInputModule
class, so that Canvas-based UI elements (_uGUI_) can be selected by
looking at them and pulling the trigger or touching the screen.
This uses the player's gaze and the magnet trigger as a raycast
generator.
Please check some tutorial regarding Google Cardboard Unity here
Please check Google-Samples posted in Github here

Moving between "pages" ( CCLayer ) in cocos2dx

I have 2 MyGameScreen objects that extends cocos2d::CCLayer. I am capturing the ccTouchesMove of the first screen so that I can create the moving effect exactly like sliding between pages of iOS application screen.
My class is like so:
class MyGameScreen: public cocos2d::CCLayer {
cocos2d::CCLayer* m_pNextScreen;
}
bool MyGameScreen::init() {
m_pNextScreen = MyOtherScreen::create();
}
void MyGameScreen::ccTouchesMoved(CCSet *touches, CCEvent *event){
// it crashes here... on the setPosition... m_pNextScreen is valid pointer though I am not sure that MyOtherScreen::create() is all I need to do...
m_pNextScreen->setPosition( CCPointMake( (fMoveTo - (2*fScreenHalfWidth)), 0.0f ) );
}
EDIT: adding clear question
It crashed when I try to setPosition on m_pNextScreen...
I have no idea why it crashed as m_pNextScreen is a valid pointer and is properly initialized. Could anybody explain why?
EDIT: adding progress report
I remodelled the whole system and make a class CContainerLayer : public cocos2d::CCLayer that contains both MyGameScreen and MyOtherScreen side by side. However, this looked like not an efficient approach, as when it grows I may need to have more than 2 pages scrollable side by side, I'd prefer to load the next page only when it is needed rather than the entire CContainerLayer that contains all the upcoming pages whether the user will scroll there or not... Do you have any better idea or github open source sample that does this?
Thank you very much for your input!
Use paging enable scrollview.download files from following link and place in your cocos2d/extenision/gui/ after that you have to set property of scrollview to enablepaging true with paging view size.
https://github.com/shauket/paging-scrollview
For Scene Transitions you can do this:
void MyGameScreen::ccTouchesMoved(CCSet *touches, CCEvent *event)
{
CCScene* MyOtherScene = CCTransitionFadeUp::create(0.2f, MyOtherScreen::scene());
CCDirector::sharedDirector()->replaceScene(MyOtherScene);
}

notifyDatasetChanged does not work on Gridview, unless devices has been rotated

This is really an odd problem. The basic gist of it is what the title says. I have an adapter, which I am updating and calling notifyDatasetChanged() The problem however, is it does not work, unless the device has been rotated at least once. I can't for the life of me figure out why, what is being done differently after a rotation occurs?
The code in question is here:
The ASyncTask that handles it..
protected void onPostExecute(ArrayList<Records> result) {
if (ca == null)
{
ca = new CoverAdapter<Records>(c, R.layout.grid_cover_with_text_item, result);
}
if (gv.getAdapter() == null)
{
gv.setAdapter(ca);
}
else
{
new AdapterHelper().update((CoverAdapter) ca, result);
ca.notifyDataSetChanged;
}
}
With "ca" being my adapter, "gv" being my GridView and AdapterHelper().update being a method I found here to clear the adapter and add all the results of the arraylist to it, so it should be being updated properly.
Remember, this code works after the device has been rotated. Very confused right now, any insight would be appreciated. Thanks in advance.
Use the debugger and step through the code to check what is expected actually happens.
Glad you found the problem... Now you can ditch the AdapterHelperclass which is a waste.

Android: code acts crazy

I have an extremely strange Problem, while trying to develop for android on eclipse
my program always returned non-sense statements, so I tried debugging it.
And Now I understand what the problem is....
...the code acts literally crazy - it's absolutely illogical
this is my code:
ArrayList<String[]> formatedVerses = readAndSplitDatabaseFile(database, books);
if (formatedVerses.size() < 1) { // check if readAndSplitDatabaseFile worked
createDatabase(database, false); // if not. the database wasnt initialized
formatedVerses = readAndSplitDatabaseFile(database, books); // try again
}
return formatedVerses;
basically I create this ArrayList
But when I debug, it doesnt just go trough the code from top to bottom, it makes weird illogical jumps
the debugger arrives at the line:
ArrayList<String[]> formatedVerses = readAndSplitDatabaseFile(database, books);
then jumps to the if-statement (so far so good)
if (formatedVerses.size() < 1) {
but "formatedVerses.size() < 1" is false, so it should jump over the if-block
but instead the debugger not only jumps INTO the if-block, but it actually jumps into THE SECOND LINE of the block:
formatedVerses = readAndSplitDatabaseFile(database, books);
which makes no sense whatsoever.
I feel like somebody is playing jokes on me.
I just cant wrap my mind around this.....
I've also tried restructuring the code...
but it only gets weirder.
I've also tried other implementations. for example like this:
if (formatedVerses.size() < 1) {
return array1;
} else{
doSomethingElse();
return array2;
}
in that case, it jumps into the true-statement of the if-block
return array1;
and then, when I click on next-step, it actually JUMPS from that line to the
return array2 line inside of the else-block, while completely leaving out the doSomethingElse()-line
anybody got any ideas?
because I'm going crazy right now...
Usually when I see behavior like this, it is because I am not running the code I think I am running. I have not done Android development, so I can't speak specifically to that, but do you have "build automatically" under the project menu checked? I have found that to reduce the number of times I see this behavior tremendously.
Have you tried cleaning the code and doing a complete rebuild? Are you running this out of Eclipse or on a remote device? If on a remote device, have you published the code to the device?
As I said, I haven't done any Android development, so some of these questions may seem obvious from an Andriod developing standpoint.
Hope those ideas help.
To me, it actually sounds like your braces are misaligned in some way. It appears to be ignoring the braces around the if statement, treating it like this.
if (formatedVerses.size() < 1) // check if readAndSplitDatabaseFile worked
createDatabase(database, false); // if not. the database wasnt initialized
formatedVerses = readAndSplitDatabaseFile(database, books); // try again
Doesn't really explain your second example though.
EDIT: Or maybe your second block is also parsing oddly.. doesn't explain why the code continues after a return statement though.
if (formatedVerses.size() < 1)
return array1;
else
doSomethingElse();
return array2;
just to elaborate on what aperkins said, which is all correct, the Eclipse debugger uses line number information from when your code was compiled into a .class file, and uses your source .java file to display what line of code is being run. So say you write this :
if (readAndSplitDatabaseFile(database, books).size() < 1) {
createDatabase(database, false); // if not. the database wasnt initialized
formatedVerses = readAndSplitDatabaseFile(database, books); // try again
}
return formatedVerses;
and you build and run, and then you add a line of code and a blank line:
ArrayList<String[]> formatedVerses = readAndSplitDatabaseFile(database, books);
if (formatedVerses.size() < 1) { // check if readAndSplitDatabaseFile worked
createDatabase(database, false); // if not. the database wasnt initialized
formatedVerses = readAndSplitDatabaseFile(database, books); // try again
}
return formatedVerses;
now when you are debugging, you are stepping through the first example, but looking at the second. The solution as aperkins mentioned is just to build and run again, or to undo your changes until they match up with what you had when you last compiled.
Okay, I figured it out.
Strangely it was something completely different.
I made some mistakes on the complete other side of my program. It had nothing to do with this code-segment (the problems weren't even in the same class.)
once that was fixed, it started to act normal again.
I don't understand it and at this point, I don't want to understand it.
Thanks for the help, everybody.

Categories

Resources