Home > Net >  How would I go about turning JavaScript into Java?
How would I go about turning JavaScript into Java?

Time:05-31

I would like to use the following code in Android studio, instead of via a web browser. However, I am having trouble converting javascript into java. Particularly the first variable that defines several other variables below. I would appreciate assistance. This is the beginning of an idle game.

main.js

 var gameData = {
        Points: 0,
        PointsPerClick: 1,
        PointsPerClickCost: 10
      }
    
      const perClickUpgrade = document.getElementById("perClickUpgrade"); 
    
      perClickUpgrade.addEventListener("click", function handleClick() { 
      
      perClickUpgrade.textContent = "Upgrade Evolution Level (Currently Level "   gameData.PointsPerClick   ") Cost: "   gameData.PointsPerClickCost   " Points";
      
      }); 
    
      function Upgrade() {
        gameData.Points  = gameData.PointsPerClick
        document.getElementById("EvolPoints").innerHTML = gameData.Points   " Evolution Points"
    
      }
    
      function BuyPointsPerClick() {
        if (gameData.Points >= gameData.PointsPerClickCost) {
          gameData.Points-= gameData.PointsPerClickCost
          gameData.PointsPerClick  = 1
          gameData.PointsPerClickCost *= 2
          document.getElementById("EvolPoints").innerHTML = gameData.Points   " Evolution Points"
        document.getElementById("perClickUpgrade").innerHTML = "Upgrade Evol Level (Currently Level "   gameData.PointsPerClick   ") Cost: "   gameData.PointsPerClickCost   " Points"
        }
    
    
        var mainGameLoop = window.setInterval(function() {
          Upgrade()
        }, 1000)
      
    function LevelTen() {
    if (gameData.Points >= 10 ) {
      alert("This is an alert message box.");  // display string message
    }
    
    }
    
      }
      function LevelTen() {
        if (gameData.PointsPerClick > 9 && gameData.PointsPerClick < 11) {
          alert("Your Creature Has Evolved to Level Ten!");  // display string message      
        }
        }

index.html

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Evolution Idle Game</title>
    <link rel="stylesheet" href="idlescreen.css">
    
  </head>
  <body>
    <body style="background-color:rgb(0, 0, 0);" ></body>
    <p id="EvolPoints" >0 Evolution Points</p>
    <button onclick="Upgrade(), LevelTen()" style="background-color:#f14e4e" >Click to Upgrade</button>
    <button onclick="BuyPointsPerClick(), LevelTen()" style="background-color:#f1bb4e" id="perClickUpgrade" >Click to Start Automation</button>
    <script src="main.js" charset="utf-8" type="text/javascript"></script>
    <img src="./Pictures/bat.png" >
  </body>
</html>

idlescreen.css

.evol-points {
  color: #ffffff
}

  .btn2 {padding: 15px 25px;
    font-size: 24px;
    text-align: center;
    text-decoration: none;
    outline: none;
    color: #fff;
    cursor: pointer;
    background-color: #4CAF50;
    border: none;
    border-radius: 15px;
    box-shadow: 0 9px #999;
  }
  
  .btn2:hover {background-color: #3e8e41}
  
  .btn2:active {
    background-color: #3e8e41;
    box-shadow: 0 5px #666;
    transform: translateY(4px);
  }

CodePudding user response:

Particularly the first variable that defines several other variables below. I would appreciate assistance.

One solution is to create a class named GameData with publicly accessible static variables:

class GameData {
    public static int points = 0;
    public static int pointsPerClick = 1;
    public static int PointsPerClickCost = 10;
}    

Now you can access these variables using the dot notation: GameData.points.

However, I should specify that this is not idiomatic Java. An idiomatic solution would create a class named GameData and then instantiate objects of that class using the new keyword.

  • Related