XCIDE
@XCIDE
Начинающий программист

Не двигается объект?

Нужно сделать так, чтобы объект двигался:
<!DOCOTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>
			GAME
		</title>
		<link rel="stylesheet" type="text/css" href="style.css">
		<script src="Scripts/Script"></script>
		<script src="Scripts/Script2"></script>
		<script src="Scripts/Script3"></script>
		<script src="Scripts/Script4"></script>
	</head>
	<body>
		<script>
			var ttt=800;
			var rrr=250;
		</script>
		<div class="hero"></div>
		<div class="kirill"></div>
		<div class="anti-kirill"></div>
		<button onclick="levo">Влево</button>
		<button onclick="ttt+=10; document.getElementById('hero').style.left=ttt+'px';">Вправо</button>
		<button onclick="rrr-=10; document.getElementById('hero').style.top=rrr+'px';">Вверх</button>
		<button onclick="rrr+=10; document.getElementById('hero').style.top=rrr+'px';">Вниз</button>
		<button>Попробовать</button>
		<button>?</button>
	</body>
</html>

body{ 
	background: url('image/fon1.png') no-repeat;
}

.hero{
	position: absolute;
	background: url('image/y3.png') ;
	background-size: contain;
	background-repeat: no-repeat;
	top: 250px;
	left: 800px;
	z-index: 1;
	width: 300px;
	height: 300px;
}


.kirill{
	position: absolute;
	background: url('image/y1.png') ; 
	background-size: contain;
	background-repeat: no-repeat;
	top: 550px;
	left: 1300px;
	z-index: 1;
	width: 300px;
	height: 300px;
}

.anti-kirill{
	position: absolute;
	background: url('image/y2.png') no-repeat;
	background-repeat: no-repeat;
	background-size: contain;
	top: 550px;
	left: 350px;
	z-index: 2;
	width: 300px;
	height: 300px;
}

var ttt=800;
	var rrr=250;
	function levo(){
		ttt-=10; document.getElementById('hero').style.left=ttt+'px';
	}

Если не сложно, объясните в чем ошибка. Я думаю что в JS, но немогу ничего сделать
  • Вопрос задан
  • 232 просмотра
Решения вопроса 1
origami1024
@origami1024
went out for a night walk
Твой объект на самом деле просто элемент.
Ошибка в том, что ты обращаешься к нему по id, но hero он только как class...

вот так попробуй:
function levo(){
    ttt-=10; document.getElementsByClassName('hero')[0].style.left=ttt+'px';
  }
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы