React-Native display user input in textboxes - android

I am pretty new to react native and not sure how to handle my approach here. I have this screen.
I want to enter points and when I click on plus or minus I want to have a record of these points in the view above.
What is the best way to handle this? I thought about making an array of textboxes and push them in every time I hit a button but I don't know where to store the array without global scope. Or I save everything in Redux but I think it doesn't make sense to put everything into Redux.
Would be happy about a suggestions, thanks :)
Project is on github: https://github.com/AlessandroVol23/Counter10000

Let's see if my idea can be helpful.
If I understood what you mean, you would like to store points that you're introducing when you click the + button and show them in the view. Also, my question is: Would you also like to show the points every time you go back to this view?
If so, my approach would be:
Storing points in an array and then save it in the redux store (This will allow you to show your points when you visit this view again)
const numbers = [40, 30, 50, 99]
Then inside the component, you only need to go through this array with a map and show the list. Something like:
const numbers = [1, 2, 3, 4, 5] -> This comes from the redux store
const listItems = numbers.map((number) =>
<li>{number}</li>
);
listItems will contain a list of number that you can then render inside the component.

Related

Kotlin button array

I really hope this question has not been answered everywhere else already but every search seems to focus on listeners and other uses of a button array but i want to also use it for formatting all buttons at the same time (activate, deactivate etc)
So here is what I have tried;
val buttons = arrayOf(btn1,btn2,btn3,btn4)
This will work, BUT will only change a single button
buttons[0].isEnabled=true; //
Then this is the bit that I am struggling with;
buttons[0..buttons.size].isEnabled=true;
The response is basically that it expects a single number and not a range.
I also tried;
buttons[].isEnabled=true;
The response is that it requires an index
I also tried
buttons.isEnabled=true;
This of course will not resolve properly
My key question really is can I apply formatting/state changes to all using an array or will I always have to do it for each button in turn?
I think it would be possible to create a loop but that isn't the route I wanted to follow here if there is an alternative
Don't think there is a way mate. You gotta loop and regardless of what syntatic sugar a language has in the end its still a for loop.
You could do:
buttons.forEach {
it.isEnabled = true
}

Can I use a variable int value inside of another variable function?

I have a pretty basic knowledge of AS3, so I'm not sure if this is actually possible, or if it is, how to do it. I am in the very starts of coding this project, so I don't actually have much code to show, but would like to figure this issue out before I start writing everything around it.
I am making a game similar to Monopoly, so I will speak in terms of that. Basically, I want 4 Players all with variable integers that reflect how much money they have. In my game, I want to be able to click on a player, click "Buy Property", and then click the property they're buying, which would be a button (btnProperty1).
Let's say Property 1 also has a purchase value, which could be "valueProperty1", and let's say the value is 100. My players will start with 1000, so I want the buying of this property to deduct 100 from the active player money. The player money int could be "moneyPlayer1, moneyPlayer2, etc.". So, I would be able to click btnPlayer1, btnBuyProperty, btnProperty1, and then moneyPlayer1 would go from 1000 to 900. To determine the active player, I had the idea of making an "activePlayer" int, and just setting it to 1, 2, 3 or 4 based on which btnPlayer I click.
In a shotty way of putting it, I could put in the btnProperty1 if (activePlayer == 1){moneyPlayer1 -= valueProperty1} and write four statements, one for each player. HOWEVER, I would like to subvert doing that by writing a function that looks like this:
moneyPlayer[activePlayer] -= valueProperty1 (if the activePlayer int is equal to 1, then it would fill in 1 to the end of moneyPlayer, therefor targeting moneyPlayer1)
In theory, it would then deduct money from whichever player is active. IF THIS IS POSSIBLE, I have no idea how to write it.
Additionally, I would like to do something similar if a subsequent player lands on an owned property. So in the same vein, I would want something that looks like this:
moneyPlayer[activePlayer] -= rentProperty1 (the rent value is deducted from the active player)
moneyPlayer[paidPlayer] += rentProperty1 (the rent value is added to the paid player)
I can figure out ways to make the active player and paid player functions work, I just need to know if I can put these variables INSIDE of other variables, so I don't have to write 18,748 if statements.
Lastly, I would love to be able to enter a player name into an input text field, then have it appear in a dynamic text field. I tried looking around and couldn't find an answer... So bonus points for this one :)
Any help would be GREATLY appreciated!! Thank you!
To answer your question, YES you can put variables "inside" another variable, this is called "classes" in AS3, and is a required thing to use whenever you plan something big. Say you want a Player class, then you can do like this:
public class Player {
private var _money:int;
... // whatever else a player has
public function get money:int { return _money;} // a property
// such functions are the way to make someone not directly steal the player's money,
// but are a way to view the money of a player
// rest of property functions goes here
public function Player() // a constructor, required
{
_money=1000; // starting money
... //etc, place default values everywhere
}
public function earnMoney(amount:int) // makes the player able to earn money
{
_money += amount; // negative money may be accepted, learn to check parameters!
}
// everything else goes here
}
Then you add there more functions that can change a player's state, say a function buy(aProperty):Boolean that would return true if the player did buy that property, and will also change the variables "inside" both the property involved and the player (this, or just call variables as if they are local).
You can do way more with classes, please read about object-oriented programming, there's a lot of basics that can help you draw your game's architecture. Learn about "encapsulation" first, to not accidentally break your objects. An example was shown above.

How to build a TinyDB with values from CheckBox and TextBox in App Inventor2?

I'm trying my best to learn App Inventor2.
Although I'm a little familiar with coding in Java with Eclipse and Android Studio and I'm aware of App Inventor limitations, I like most, the graphical interface and the visual objects, than developing an app all the way in code.
I'm having a screen in an app as the following image, that I need to achieve this functionality:
a. User checks a "CheckBox" related to an image of their liking.
b. User inputs some identification related (id1, id2) to this image.
c. Clicks the "Save" button to store this values (checkBox, id1,id2) to a TinyDB.
d. Clicks the "Reset" button to clear info stored in TinyDB (checkBox, id1,id2).
I know that TinyDB can only store text. I have tried to make a TinyDB with a tag as a list named "idList" and have it populated with the values from "checkBox", "id1","id2", without success. I believe that I'm knot checking weather the "checkBox" changed or something like that.
Can someone be kind enough to point me to the right direction following this logic, or point me to something better if I'm wrong?
Thank you all in advance for your answers.
in the button click event you need some logic to find out, which checkbox has been checked, something like this (pseudocode)
if checkbox1.checked then store the text 1,
elseif checkbox2.checked then store the text 2,
etc.
in TinyDB you can store a list with the result of that logic and the text from the textboxes like this
also you have to read the values from TinyDB again. Normally you do this in the Screen.Initialize event, example:
in case you do not like the advanced blocks, you can do the same with some regular blocks, too...
See also
How to work with Lists by Saj
How to work with Lists and Lists
of lists (pdf) by appinventor.org

AS3 Game, Collecting points, multiple movie clips with the same instance name, not working

I've decided to create an Android touch screen game. I am a complete and utter beginner and am learning as I go.
My game has a little elephant that moves up when you press and hold on the screen and falls when there is no contact with the screen. The aim is to collect as many peanuts that fly past as possible to gain the highest score. Pretty simple, you'd think so.
So far, I've managed to get to the point where the elephant can collide with a peanut and the peanut disappears.
My issue right now is, I can't create more than one peanut, with the same instance name of "peanut" because only the one will work and the others will not be recognized. I've done a good ole google search and nothing has really given me the right way to go. Could someone give me a clear answer of what to do or where to go from here?
If you need any more info, the code or a picture of what i've got so far to help you understand just let me know :)
Samantha
Instance name must be unique, and you cannot use instance name to find a set of movie clips. You should instead use an array, and at creating a peanut add it there too using say push(), and at collecting a peanut, splice it out.
In fact, whenever you get a multi-instance class with similar functionality (aka "collect"), use an Array to store references to all of these, so you will always know that ALL of your instances are accessible through that array.
How to work with arrays
A sample code:
var peanuts:Array=new Array();
function addPeanut(x:Number,y:Number):void {
var peanut:Peanut=new Peanut(); // make a peanut, you do this somewhere already
peanut.x=x;
peanut.y=y;
peanuts.push(peanut); // this is where the array plays its role
game.addChild(peanut); // let it be displayed. The "game" is whatever container
// you already have to contain all the peanuts.
}
function removePeanut(i:int):void {
// give it index in array, it's better than giving the peanut
var peanut:Peanut=peanuts[i]; // get array reference of that peanut
peanuts.splice(i,1); // remove the reference from the array by given index
game.removeChild(peanut); // and remove the actual peanut from display
}
function checkForPeanuts():void {
// call this every so often, at least once after all peanuts and player move
for (var i:int=peanuts.length-1; i>=0; i--) {
// going through all the peanuts in the array
var peanut:Peanut=peanuts[i];
if (player.hitTestObject(peanut)) {
// of course, get the proper reference of "player"!
// YAY got one of the peanuts!
// get some scoring done
// get special effects, if any
removePeanut(i); // remove the peanut
}
}
}

Android: Button to new screen and show sql data

I am working a project. I have lots of word text data and don't want to add layout and class for each one. I want to use sqlite, but don't know how. For example, there is a button, id is cat.
When clicking the cat button, a new screen opens and shows information of cat. One layout, one class, but lots of screens. The data must come from sql because there are too many animals.
Thanks in advance.
You have given the answer already yourself: SQL database may be your friend. Work yourself through this tutorial and see how far that brings you. You'll have to manipulate the UI from code, that is doable as well.
Another approach you might want to consider is putting everything in XML into strings.xml file(s). This depends a bit on the size and structure of your data. From code you can reach this using getString( R.id.blah ).
Call other screen passing values.
In the button click do:
startActivity(new Intent(this, SecondScreen.class).putExtra('Identify', 'Value'));
In the Second Screen do:
String value = getIntent().getStringExtra('Identify');
Now, take that value and make the query in SqLite and display the result.

Categories

Resources